现在我用反射“PersonModel.dll",调用一个类型方法:GetAllPersons(),返回Person[],其中Person为“PersonModel.dll"在定义,请问,我要怎么操作才能取回返回的数组值呢?急~~~,在线等,谢谢了
解决方案 »
- 写一条sql语句怎样把它放到table中
- c#连接数据库问题
- SharpDevelop中怎么把保留字自动转换大小写,如console自动转换为Console
- webbrowser显示问题!急!!!
- nextmatch()不能循环了
- 接触C#的服务器端正则表达式相关验证
- 为什么我把textbox绑定DataTable的字段以后直接更新textbox的text,但用dataset.getchanges()取出来的值是空
- NPOI对excel的相关操作的问题。。。。
- OledbConnection的问题
- C# dataGridView 读取Excel 表格内容 报错:对象不能从DBNUll转换为其他类型
- js如何获取treeview选中节点的值
- dataGridView ComboBoxColumns问题
System.Reflection.MethodInfo mi = Parser.GetType().GetMethod("Save", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
2、 调用方法并传递其参数
mi.Invoke(Parser, new Object[] { ....});
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});
}
}
我给你写了个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