.net 中使用InvokeMember, 反射来调用 成员及成员函数。

 

using System;

using System.Reflection;

// This sample class has a field, constructor, method, and property.

class MyType

{

Int32 myField;

public MyType(ref Int32 x) { x *= 5; }

public override String ToString() { return myField.ToString(); }

public Int32 MyProp

{

get { return myField; }

set

{

if (value < 1)

throw new ArgumentOutOfRangeException("value", value, "value must be > 0");

myField = value;

}

}

}

class MyApp

{

static void Main()

{

Type t = typeof(MyType);

// Create an instance of a type.

Object[] args = new Object[] { 8 };

Console.WriteLine("The value of x before the constructor is called is {0}.", args[0]);

Object obj = t.InvokeMember(null,

BindingFlags.DeclaredOnly |

BindingFlags.Public | BindingFlags.NonPublic |

BindingFlags.Instance | BindingFlags.CreateInstance, null, null, args);

Console.WriteLine("Type: " + obj.GetType().ToString());

Console.WriteLine("The value of x after the constructor returns is {0}.", args[0]);

// Read and write to a field.

t.InvokeMember("myField",

BindingFlags.DeclaredOnly |

BindingFlags.Public | BindingFlags.NonPublic |

BindingFlags.Instance | BindingFlags.SetField, null, obj, new Object[] { 5 });

Int32 v = (Int32)t.InvokeMember("myField",

BindingFlags.DeclaredOnly |

BindingFlags.Public | BindingFlags.NonPublic |

BindingFlags.Instance | BindingFlags.GetField, null, obj, null);

Console.WriteLine("myField: " + v);

// Call a method.

String s = (String)t.InvokeMember("ToString",

BindingFlags.DeclaredOnly |

BindingFlags.Public | BindingFlags.NonPublic |

BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, null);

Console.WriteLine("ToString: " + s);

// Read and write a property. First, attempt to assign an

// invalid value; then assign a valid value; finally, get

// the value.

try

{

// Assign the value zero to MyProp. The Property Set

// throws an exception, because zero is an invalid value.

// InvokeMember catches the exception, and throws

// TargetInvocationException. To discover the real cause

// you must catch TargetInvocationException and examine

// the inner exception.

t.InvokeMember("MyProp",

BindingFlags.DeclaredOnly |

BindingFlags.Public | BindingFlags.NonPublic |

BindingFlags.Instance | BindingFlags.SetProperty, null, obj, new Object[] { 0 });

}

catch (TargetInvocationException e)

{

// If the property assignment failed for some unexpected

// reason, rethrow the TargetInvocationException.

if (e.InnerException.GetType() !=

typeof(ArgumentOutOfRangeException))

throw;

Console.WriteLine("An invalid value was assigned to MyProp.");

}

t.InvokeMember("MyProp",

BindingFlags.DeclaredOnly |

BindingFlags.Public | BindingFlags.NonPublic |

BindingFlags.Instance | BindingFlags.SetProperty, null, obj, new Object[] { 2 });

v = (Int32)t.InvokeMember("MyProp",

BindingFlags.DeclaredOnly |

BindingFlags.Public | BindingFlags.NonPublic |

BindingFlags.Instance | BindingFlags.GetProperty, null, obj, null);

Console.WriteLine("MyProp: " + v);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值