我開始在.Net中使用動態對象,並且我無法弄清楚如何執行某些操作。獲取通用類型的調用動態對象中的方法
我有一個繼承自DynamicObject的類,並且我重寫了TryInvokeMember方法。
例如
class MyCustomDynamicClass : DynamicObject
{
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
// I want to know here the type of the generic argument
}
}
這方法我想知道在調用泛型參數的類型(如果有的話)內。
例如 如果我調用下面的代碼,我想我的動態對象
dynamic myObject = new MyCustomDynamicClass();
myObject.SomeMethod<bool>("arg");
myObject.SomeOtherMethod<int>("arg");
目前的overrided方法內部System.Boolean和System.Int32的價值,如果我把一個斷點overrided方法,我可以進去被調用的方法名稱(「SomeMethod」和「SomeOtherMethod」,以及參數值,但不是泛型類型)。
如何獲得這些值?
謝謝!
您很可能需要使用反射來查找該方法。 MethodInfo提供對泛型類型參數的訪問。 – 2011-03-30 21:10:34
問題是該方法不存在,我只能訪問binder對象,它有一個CallInfo屬性,它沒有任何泛型信息。 – willvv 2011-03-30 21:26:17
你知道,我一直在試用這個樣品一段時間,而且我也找不到通用信息。這實際上是一個非常好的問題。 – Tejs 2011-03-30 21:35:18