简体   繁体   English

无法将类型为“ System.Int32”的对象转换为类型为“ System.Reflection.RuntimePropertyInfo”

[英]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.

相关问题 无法转换&#39;System.Reflection.RuntimePropertyInfo&#39;类型的对象 - Unable to cast object of type 'System.Reflection.RuntimePropertyInfo' 无法将类型为“ System.Reflection.RuntimePropertyInfo”的对象转换为类型为“ System.IConvertible”的对象 - Unable to cast object of type 'System.Reflection.RuntimePropertyInfo' to type 'System.IConvertible' 错误“无法将'System.Int32'类型的对象强制转换为'System.Collections.Generic.List`1 [System.Int32]'' - error “Unable to cast object of type 'System.Int32' to type 'System.Collections.Generic.List`1[System.Int32]'” C#:无法将“System.Int64”类型的对象转换为“System.Int32”类型 - C#: Unable to cast object of type 'System.Int64' to type 'System.Int32' 错误:System.InvalidCastException:无法将“System.Byte”类型的对象转换为“System.Int32”类型 - Error: System.InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.Int32' 无法将 System.Int64 类型的 object 转换为 System.Int32 类型 - Unable to cast object of type System.Int64 to type System.Int32 Entityframework 核心无法将类型为“System.Int32”的 object 转换为类型“System.Int64” - Entityframework Core Unable to cast object of type 'System.Int32' to type 'System.Int64' 无法将类型为&#39;System.Data.EnumerableRowCollection`1 [System.Int32]&#39;的对象强制转换为&#39;System.IConvertible&#39; - Unable to cast object of type 'System.Data.EnumerableRowCollection`1[System.Int32]' to type 'System.IConvertible' System.InvalidCastException:无法将“System.Double”类型的 object 转换为代码中的“System.Int32”类型 - System.InvalidCastException: Unable to cast object of type 'System.Double' to type 'System.Int32' in code 无法将类型为“ System.Int64”的对象转换为类型为“ System.Int32”的对象 - Unable to cast object of type 'System.Int64' to type 'System.Int32'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM