[英]Unable to cast object of type 'System.Int32' to type 'System.Reflection.RuntimePropertyInfo'
I have several entities that I'd like to transform. 我有几个要转换的实体。 Here is an example:
这是一个例子:
public class FromClass
{
public string Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string TimeStamp { get; set; }
}
public class ToClass
{
public int Id { get; set; }
public string Name { get; set; }
public int TypeId { get; set; }
public DateTime TimeStamp { get; set; }
}
I've made a class of how the transformation should be done for each property like this: 我制作了一类关于如何对每个属性进行转换的类,如下所示:
public interface ITransformationRule
{
T Transform<T>(string value);
}
public class ColumnDescription
{
public string SourceColumnName { get; set; }
public string TargetObjectProperty { get; set; }
public ITransformationRule TransformationRule { get; set; }
}
The source properties are always strings and the data is validated in a previous step so I know I can cast without exceptions. 源属性始终是字符串,并且数据已在上一步中进行了验证,因此我知道我可以无例外地进行转换。 So for each property I have a transformationrules.
因此,对于每个属性,我都有一个转换规则。 Some transformations are plain casts while other do lookup in tables.
一些转换是普通转换,而其他转换则在表中查找。
In the above example I'd have a List of ColumnDescriptions like this: 在上面的示例中,我将有一个ColumnDescriptions列表,如下所示:
public List<ColumnDescription> TransformationDescription = new List<ColumnDescription>
{
new ColumnDescription{SourceColumnName = "Id", TargetObjectProperty = "Id", TransformationRule = new IntegerTransformation() }
};
etc... Now here is where I'm lost (or perhaps the ITransformationRule interface should look a little bit different). 等等...现在这是我迷路的地方(或者ITransformationRule接口应该看起来有点不同)。 I've written the IntegerTransformationClass like this:
我已经这样写了IntegerTransformationClass:
public class IntegerTransformation : ITransformationRule
{
public T Transform<T>(string value)
{
object returnvalue = int.Parse(value);
return (T) returnvalue;
}
}
finally I'm looping through the properties in the list like this: 最后,我循环遍历列表中的属性,如下所示:
foreach (var row in TransformationDescription)
{
¨...
var classType = row.TransformationRule.GetType();
var methodInfo = classType.GetMethod("Transform");
var generic = methodInfo.MakeGenericMethod(toProp.GetType());
var parameters = new object[] { toProp.ToString() };
var toValue = generic.Invoke(specialTransform.TransformationRule, parameters);
toProp.SetValue(toObj, Convert.ChangeType(toValue, toProp.PropertyType), null);
}
At runtime a get the exeption.Unable to cast object of type 'System.Int32' to type 'System.Reflection.RuntimePropertyInfo' when returning from the TransformationClass. 在运行时获取实例。从TransformationClass返回时,无法将类型为“ System.Int32”的对象转换为类型为“ System.Reflection.RuntimePropertyInfo”。
Perhaps I'm going about the problem in a totaly wrong way. 也许我以一种完全错误的方式来解决这个问题。 Any input would be appreciated.
任何输入将不胜感激。
This line is the problem: 这行是问题所在:
var generic = methodInfo.MakeGenericMethod(toProp.GetType());
You're calling GetType()
on toProp
- that will return some type derived from PropertyInfo
. 您在
toProp
上调用GetType()
-这将返回派生自PropertyInfo
某种类型。 You actually want the property type, so just change it to: 您实际上需要属性类型,因此只需将其更改为:
var generic = methodInfo.MakeGenericMethod(toProp.PropertyType);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.