jeudi 14 janvier 2016

Method Invoke with complex parameter dynamically (C#)

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





Aucun commentaire:

Enregistrer un commentaire