现在我用反射“PersonModel.dll",调用一个类型方法:GetAllPersons(),返回Person[],其中Person为“PersonModel.dll"在定义,请问,我要怎么操作才能取回返回的数组值呢?急~~~,在线等,谢谢了

解决方案 »

  1.   

    1、首先获取方法类似
      System.Reflection.MethodInfo mi = Parser.GetType().GetMethod("Save", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
    2、  调用方法并传递其参数
    mi.Invoke(Parser, new Object[] { ....});
      

  2.   

    下面是msdn上的部分代码
        class MyMainClass
        {
            static void Main()
            {
                // Get the type of MySimpleClass.
                Type myType = typeof(MySimpleClass);            // Get an instance of MySimpleClass.
                MySimpleClass myInstance = new MySimpleClass();
                MyCustomBinder myCustomBinder = new MyCustomBinder();            // Get the method information for the particular overload 
                // being sought.
                MethodInfo myMethod = myType.GetMethod("MyMethod", 
                    BindingFlags.Public | BindingFlags.Instance,
                    myCustomBinder, new Type[] {typeof(string), 
                        typeof(int)}, null);
                Console.WriteLine(myMethod.ToString());
                
                // Invoke the overload.
                myType.InvokeMember("MyMethod", BindingFlags.InvokeMethod, 
                    myCustomBinder, myInstance, 
                        new Object[] {"Testing...", (int)32});
            }
        }
      

  3.   

    现在有时间,
    我给你写了个Demo,你看看:PersonModel.dll:using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace PersonModel
    {
        public class Class1
        {        public Person[] GetAllPersons()
            {
                Person[] p = new Person[]
                {
                new Person(){Name="aaa",Age=20},
                new Person(){Name="bbb",Age=21},
                new Person(){Name="ccc",Age=26}
                };
                return p;
            }
        }
        public class Person
        {
            public string Name { get; set; }        public int Age { get; set; }
        }
    }
    测试类:using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Assembly assembly = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "PersonModel.dll");
                object instance = assembly.CreateInstance("PersonModel.Class1");
                MethodInfo method = instance.GetType().GetMethod("GetAllPersons");
                Array result = (Array)method.Invoke(instance, null);
                
                foreach (dynamic item in result)
                {                
                    Console.WriteLine(item.Name + "  " + item.Age);
                }           
              
                Console.Read();
            }
            
            
        }
    }
    输出:aaa  20
    bbb  21
    ccc  26