天天看點

C#反射 建立對象,調用方法

namespace TestReflection

{

public class MyCls

{

public void Fun()

{

Console.WriteLine("MyCls.Fun invoke.");

}

}

}

/////////////////////////////////////////////////////////

...

using System.Reflection;

class Program

static void Main(string[] args)

Type type = Assembly.Load("TestReflection").GetType("TestReflection.MyCls");

object obj= Activator.CreateInstance(type);

type.InvokeMember("Fun", BindingFlags.Default | BindingFlags.InvokeMethod,

null, obj, new object[] { });

Console.ReadKey();