Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8716947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:12:07+00:00 2026-06-13T06:12:07+00:00

I have a DynamicObject-based class that implements an interface dynamically (via TryGet* and TryInvoke*).

  • 0

I have a DynamicObject-based class that implements an interface dynamically (via TryGet* and TryInvoke*). I want to call the methods that it implements using reflection. The problem is that it throws a TargetException (Object does not match target type).

Like I said, it’s dynamic, so it forwards the calls via a series of connections as text to another program, which then calls the destination method using reflection. It is basically a proxy class. The method names are known at compile time (because of the interface), but are called externally.

typeof(ITelescope).GetMethod(Console.ReadLine()).Invoke(prox,null);

ITelescope is the interface, prox is the dynamic object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:12:08+00:00Added an answer on June 13, 2026 at 6:12 am

    Although you can not invoke a dynamic method with reflection, you can invoke TryGet…/TryInvoke… methods of DynamicObject which in turn execute your dynamic method…

    dynamic obj = new MyDyn();
    Console.WriteLine(obj.Text);
    
    string methodName = "YourDynamicMethod";                    
    
    var p1 = new ParameterModifier(2);
    p1[0] = false; p1[1] = true;
    
    var args = new object[] { new MemberBinder(methodName, true), null };
    
    var res = typeof(DynamicObject).InvokeMember(
        "TryGetMember",
        BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public,
        null,
        obj,
        args,
        new ParameterModifier[] { p1 },
        null,
        null);
    
    var result = args[1];
    

    public class MyDyn : DynamicObject
    {
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = "#" + binder.Name + "#";
            return true;
        }
    }
    
    public class MemberBinder : GetMemberBinder
    {
        public MemberBinder(string name, bool ignoreCase) : base(name, ignoreCase)
        {
        }
    
        public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
        {
            throw new NotImplementedException();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# class that inherits from DynamicObject and using serialization is creating
I have written an abstract class that I am using to automate a lot
Creating a class that implements DynamicObject public class Test : DynamicObject { public override
I want to have dynamic parameters, so I made these models : class DynamicObject(models.Model):
I have a DynamicObject and I want it to be castable to IDictionary, exactly
i have a student class. class student : DynamicObject { public Dictionary<string, object> dics
Just to be clear, a class that inherits DynamicObject (in C# of course) is
I have a number of data access methods that accept a dynamic object parameter
I have a situation where I have a class that accepts an instance of
I have a DynamicObject wrapper that is basically forwarding property values from another private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.