C#中类的反射
在我们不知道类内的属性名和方法时,可以通过反射的方式访问类内的属性和方法,操作如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;//引入反射命名空间
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Reflection
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//属性的访问
//静态属性的访问
Console.WriteLine("Initial value of StaticProperty:" + Example.StaticProperty);
Type exampleType = typeof(Example);
PropertyInfo examplePropertyInfo = exampleType.GetProperty("StaticProperty");
//不通过构造对象直接更改静态属性的值
examplePropertyInfo.SetValue(null, 70);
Console.WriteLine("New1 value of StaticProperty:"+examplePropertyInfo.GetValue(null));
//通过构造对象后更改静态属性的值
Example staticExample = new Example();
examplePropertyInfo.SetValue(staticExample, 75);
Console.WriteLine("New2 value of StaticProperty:"+examplePropertyInfo.GetValue(staticExample));
//实例属性的访问
Example instanceExample = new Example();
Console.WriteLine("Initial value of InstanceExample:" + instanceExample.InstanceProperty);
examplePropertyInfo = exampleType.GetProperty("InstanceProperty");
examplePropertyInfo.SetValue(instanceExample, 80);
Console.WriteLine("New value of InstanceExample:"+examplePropertyInfo.GetValue(instanceExample));
//属性相同的两个类通过遍历属性进行属性赋值
Type example1Type = typeof(Example1);
foreach(PropertyInfo item in example1Type.GetProperties())
{
Type exampleType1 = typeof(Example);
Example example = new Example();
Example1 example1 = new Example1();
Console.WriteLine("Initial value of "+item.Name+" :" + item.GetValue(example1));
item.SetValue(example1, exampleType1.GetProperty(item.Name.ToString()).GetValue(example));
Console.WriteLine("New value of "+item.Name+" :"+item.GetValue(example1));
}
//方法的访问
Example methodExample = new Example();
Type methodExampleType = methodExample.GetType();
//静态方法的调用
MethodInfo staticMethodInfo = methodExampleType.GetMethod("StaticShow");
staticMethodInfo.Invoke(methodExample, null);
staticMethodInfo.Invoke(null, null);
//非静态方法的调用
MethodInfo noStaticMethodInfo =methodExampleType.GetMethod("NoStaticShow");
noStaticMethodInfo.Invoke(methodExample,null);
//带参数方法的调用
MethodInfo paramMethodInfo = methodExampleType.GetMethod("ParamShow");
paramMethodInfo.Invoke(methodExample, new object[] { "ParamMethodFunc" });
//带返回值的方法调用
MethodInfo returnMethodInfo = methodExampleType.GetMethod("returnAdd");
object result=returnMethodInfo.Invoke(methodExample, new object[] { 1, 2 });
Console.WriteLine(result);
Console.ReadKey();
}
}
class Example
{
private static int _staticProperty = 10;
private int _instanceProperty = 20;
public static int StaticProperty
{
get { return _staticProperty; }
set { _staticProperty = value; }
}
public int InstanceProperty
{
get { return _instanceProperty; }
set { _instanceProperty = value; }
}
public void NoStaticShow()
{
Console.WriteLine("I am NoStatic Method");
}
public static void StaticShow()
{
Console.WriteLine("I am Static Mehtod");
}
public void ParamShow(string msg)
{
Console.WriteLine("I am ParamShow:"+msg);
}
public int returnAdd(int a,int b)
{
return a + b;
}
}
class Example1
{
private static int _staticProperty = 10;
private int _instanceProperty = 20;
public static int StaticProperty
{
get { return _staticProperty; }
set { _staticProperty = value; }
}
public int InstanceProperty
{
get { return _instanceProperty; }
set { _instanceProperty = value; }
}
}
}
运行结果: