【发布时间】:2012-04-06 11:18:36
【问题描述】:
我有一个特定类型的对象 (SpecialImage),它将 隐式运算符 实现为另一种类型 (Image)。
SpecialImage不是派生自Image。但是可以通过操作符进行以下操作:
var someImage = new Image();
(SpecialImage)someImage;
我有一个带有属性的对象,我通过反射和Image 对象循环遍历:
是否可以在尝试设置值之前检查对象是否可转换为 info.PropertyType?
var someImage = new Image();
foreach(PropertyInfo info in someOjbect.GetType().GetProperties()) {
//info.PropertyType == typeof(SomeImage);
//Is it possible to check if the object is castable to
//info.PropertyType before trying to set the value?
info.SetValue(someObject, someImage, null);
}
【问题讨论】:
标签: c# reflection casting operator-keyword propertyinfo