我正在开发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
}
希望这个问题不是太复杂。
请注意,无论您选择哪种解决方案,您都无法在此获取类型安全代码,因为编译器无法在编译时确定实际对象的类型。 –