![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1.首先建立一个类
class PropertyClass
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public PropertyClass()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
public PropertyClass(ref Int32 x)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
x *= 5;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int fieldValue = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string name;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Name
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return name; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ name = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int GetInt(int value)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return value;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Display(string str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(str);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override String ToString()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return fieldValue.ToString();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2.用反射的方式调用这个PropertyClass类中的方法
public static void instanceLove()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
PropertyClass propertyClass = new PropertyClass();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Type type = propertyClass.GetType();
//Type type = typeof(propertyClass);效果和上面语句一样,只是写法不同。
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
object obj = type.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, new object[]
{ });
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//调用没有返回值的方法
//运行之后的结果[Hello World]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
type.InvokeMember("Display", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, obj, new object[]
{ "Hello World" });
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//调用有返回值的方法
//运行之后的结果[1982]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int i = (int)type.InvokeMember("GetInt", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, obj, new object[]
{ 1982 });
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置属性值
//此时的Name 's Value为[My Name is Brian Lei]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
type.InvokeMember("Name", BindingFlags.SetProperty, null, obj, new object[]
{ "My Name is Brian Lei" });
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//获取属性值
//运行之后的结果[My Name is Brian Lei]
string str = (string)type.InvokeMember("Name", BindingFlags.GetProperty, null, obj, null);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置字段值
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
type.InvokeMember("fieldValue", BindingFlags.SetField, null, obj, new object[]
{ 813 });
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//获取字段值
//运行之后的结果[813]
int f = (int)type.InvokeMember("fieldValue", BindingFlags.GetField, null, obj, null);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//调用override方法
//运行之后的结果[813]
String s = (String)type.InvokeMember("ToString", BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, null);
//调用构造方法
//运行之后的结果[8*5=40]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Object objs = type.InvokeMember(null,BindingFlags.DeclaredOnly |BindingFlags.Public | BindingFlags.NonPublic |BindingFlags.Instance | BindingFlags.CreateInstance, null, null, new Object[]
{ 8 });
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3.写个Main函数测试一下
class EntryPoint
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
static void Main()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Test.instanceLove();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted @
2007-03-14 17:18
RicoRui
阅读(
527)
评论()
编辑
收藏
举报