1

I want to invoke method1 in class1 dynamically. I don't want to use Prm class directly. How to send p parameter dynamically. Here is a way but i could'nt manage

namespace WindowsFormsApplicationMethodCall
{
    public class Prm
    {
        public string p1 { get; set; }
        public long p2 { get; set; }
    }
    public class Class1
    {
        public string Method1(Prm p)
        {
            return "Hello world";
        }
    }
}

usage:

private void button1_Click(object sender, EventArgs e)
{
  var ass = Assembly.LoadFrom("abc.dll");
  var business = ass.CreateInstance("WindowsFormsApplicationMethodCall.Class1");
  var mInfo = business.GetType().GetMethod("Method1");
  var anonymParameter = new { p1 = "", p2 = 2 };
  mInfo.Invoke(business,new object[]{ anonymParameter});
  //var res = mInfo.TolerantCast(new { p1 = "", p2 = 2 });
  //var param = new Dictionary<string,object>();
  //param.Add("p", result);                
}

I found this error : Object of type <>f__AnonymousType0`2[System.String,System.Int32] cannot be converted to type

6
  • must be create object parameter same type Prm this code create anonymous object "var anonymParameter = new { p1 = "", p2 = 2 };" Commented Jan 14, 2016 at 11:53
  • 3
    c#/.Net is type safe. The functions only accepts Prm objects, so you have to pass a Prm object. Commented Jan 14, 2016 at 11:55
  • I know this. Actually i have method, class and dll names as string. And there are parameters as List<string>. And i want to invoke method1 dynamically. Commented Jan 14, 2016 at 12:10
  • try use dynamic anonymParameter = ass.CreateInstance("WindowsFormsApplicationMethodCall.Prm"); anonymParameter.p1=""; or var anonymParameter = ass.CreateInstance("WindowsFormsApplicationMethodCall.Prm"); anonymParameter.GetType().GetProperty("p1").SetValue(anonymParameter,""); anonymParameter.GetType().GetProperty("p2").SetValue(anonymParameter,2); anonymParameter.p2=2; Commented Jan 14, 2016 at 12:46
  • yes, I think there is'nt another way. Can you copy these code in your answer. Commented Jan 14, 2016 at 13:36

2 Answers 2

2

Create Instance From Class "Prm" and use it Instead

var anonymParameter = new { p1 = "", p2 = 2 };

to

var anonymParameter  = ass.CreateInstance("WindowsFormsApplicationMethodCall.Prm");
anonymParameter.GetType().GetProperty("p1").SetValue(anonymParameter,""); 
anonymParameter.GetType().GetProperty("p2").SetValue(anonymParameter,2); 
Sign up to request clarification or add additional context in comments.

1 Comment

yes. I create instance and set value via GetPropeties() and SetValue()
1

You need to pass an object of type Prm:

var parameter = new Prm { p1 = "", p2 = 2 };
mInfo.Invoke(business,new object[]{ parameter });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.