类似这个,把类换成Delegate试试
// cs_operator_typeof.cs
// Using typeof operator
using System;
using System.Reflection;public class MyClass
{
public int intI;
public void MyMeth()
{
} public static void Main()
{
Type t = typeof(MyClass); // alternatively, you could use
// MyClass t1 = new MyClass();
// Type t = t1.GetType(); MethodInfo[] x = t.GetMethods();
foreach (MethodInfo xtemp in x)
{
Console.WriteLine(xtemp.ToString());
} Console.WriteLine(); MemberInfo[] x2 = t.GetMembers();
foreach (MemberInfo xtemp2 in x2)
{
Console.WriteLine(xtemp2.ToString());
}
}
}
// cs_operator_typeof.cs
// Using typeof operator
using System;
using System.Reflection;public class MyClass
{
public int intI;
public void MyMeth()
{
} public static void Main()
{
Type t = typeof(MyClass); // alternatively, you could use
// MyClass t1 = new MyClass();
// Type t = t1.GetType(); MethodInfo[] x = t.GetMethods();
foreach (MethodInfo xtemp in x)
{
Console.WriteLine(xtemp.ToString());
} Console.WriteLine(); MemberInfo[] x2 = t.GetMembers();
foreach (MemberInfo xtemp2 in x2)
{
Console.WriteLine(xtemp2.ToString());
}
}
}
这里也可以是DLL.我不知道你是不是要实现这样.CreateInstance便可以实例化一个类了.
实例好的类实例用GetType().GetMethod使用其中的一个方法.似乎不管这个方法是否是private.这个很流氓的.我觉得.HEHE.
Assembly assm = Assembly.LoadFrom(@"C:\begcsharp\chapter2 \DGTripleMasterDetails\bin\Debug\DGTripleMasterDetails.exe");
object objForm = assm.CreateInstance("DGTripleMasterDetails.Form1");
MethodInfo mi = objForm.GetType().GetMethod("GetGrandChildTable",BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static);
object obj = mi.Invoke(objForm,null);
DataTable dt = (DataTable)mi.Invoke(assm,null);
我用DATATABLE,是因为GetGrandChildTable返回的是该类型的.
using System.Reflection;namespace JoyCode.QQChen.CSharp {
public delegate string Foo(int i, object obj); public class StrTest {
public static void Main() {
Type t = typeof(Foo);
MethodInfo m = t.GetMethod("Invoke");
Console.WriteLine("Delegate Return Type: " + m.ReturnType);
ParameterInfo[] pinfos = m.GetParameters();
Console.Write("Delegate Parameters: (");
for(int i = 0; i < pinfos.Length; ++ i) {
Console.Write(pinfos[i].ParameterType);
if (i != pinfos.Length - 1) Console.Write(", ");
}
Console.WriteLine(")");
}
}
}