别人写的关于反射的项目见Github,觉得挺有意思的:https://github.com/ReflectionMagic/ReflectionMagic
使用:
现有一个类,如下:
namespace ReflectionMagic.Test
{
class Test
{
private string A()
{
return "A";
}
public string B()
{
return "B";
}
private static string C()
{
return "C";
}
}
}
调用A,B,C方法如下:
class Program
{
static void Main(string[] args)
{
Test test = new Test();
Console.WriteLine(test.AsDynamic().A());
Console.WriteLine(test.AsDynamic().B());
var dynamicType = Assembly.GetExecutingAssembly().GetDynamicType("ReflectionMagic.Test.Test");
Console.WriteLine(dynamicType.C());
Console.Read();
}
}