我正在開發ASP.NET Web窗體項目,並創建了自定義事件處理函數。如何使用Type對象將對象類型的對象轉換爲Type對象引用的類型
potected void MyEventHandler(object sender, EventArgs e)
{
var actualObject = (ActualObject) sender;
// I don't want to do what is in the line above. I want to use my EventArgs
// parameter 'e' which contains a property of type Type to cast the sender
// object into the underlying type defined in the EventArg property.
// I want to do something like this ...
var actuaObject = (typeof(e.TypeProperty)) sender;
//i.e. I don't want to explicitly reference the type to which
//I'm trying to cast between the parentheses
}
希望這個問題不是太複雜。
請注意,無論您選擇哪種解決方案,您都無法在此獲取類型安全代碼,因爲編譯器無法在編譯時確定實際對象的類型。 –