使用反射获取通用列表始终为null

时间:2016-01-08 15:07:44

标签: c# reflection

问题:

嘿伙计们,我有一个奇怪的问题,开始真的让我感到沮丧。我已经搜索了高低的答案,并尝试了很多不同的东西,但由于某种原因,我无法使这段代码正常工作。

我使用反射来根据 T 的类型将匿名对象数组转换为特定类型。现在提到它之前,是反射是我正在做的事情的唯一方法(将脚本语言转换为Type Strong C#对象)。

我的问题在第35-43行。如果值类型是List {T}的值,那么我首先检查它是否为空...如果是,我使用Activator.CreateInstance()创建一个新列表(第58行),否则使用现有列表。我在这里面临的问题是,当调用此方法时,值总是为NULL,即使我在第43行设置了值。这意味着列表中的列表永远不会超过1个,即使我添加更多比1项...

在第23和51行,您可以看到我为其他类型设置了值,并且它运行正常,所以我在第34-43行上做错了什么阻止了这段代码正常工作?

守则:

您可以使用pastebin或以下的行号查看此代码:

public class ObjectProperty<T> : ObjectPropertyBase
{
    /// <summary>
    /// Gets or Sets the value for this property
    /// </summary>
    public T Value;

    /// <summary>
    /// Takes an array of string values, and converts it to the proper value type for
    /// this instance's Generic Type
    /// </summary>
    /// <param name="ValueParams">The string value's to convert, and set the
    /// Value of this instance to.
    /// </param>
    public override void SetValueFromParams(String[] ValueParams)
    {
        Type PropertyType = typeof(T);

        // Check for array's, as they are handled differently
        if (PropertyType.IsArray)
        {
            // Set the value to the instanced object
            Value = (T)ConvertArray(ValueParams, PropertyType);
        }
        else if (PropertyType.IsGenericType)
        {
            // Grab our types interfaces and generic types
            Type[] interfaces = PropertyType.GetInterfaces();
            Type[] types = PropertyType.GetGenericArguments();

            // Check for List<T>
            if (interfaces.Any(i => i == typeof(IList)))
            {
                // Grab our current list... if the Value isnt created yet, make it
                dynamic obj = (dynamic)Value ?? CreateList(types);

                // Add our value to the list
                if (types[0].IsArray)
                    obj.Add(ConvertArray(ValueParams, types[0]));
                else
                    obj.Add(ConvertValue<dynamic>(ValueParams[0], types[0]));

                Value = (T)obj;
            }
            else
                throw new Exception($"Invalid Generic Type found \"{PropertyType}\"");
        }
        else
        {
            // Since we are not an array, extract our only value
            Value = ConvertValue<T>(ValueParams[0], PropertyType);
        }
    }

    protected static IList CreateList(Type[] genericTypes)
    {
        Type obj = typeof(List<>).MakeGenericType(genericTypes);
        return (IList)Activator.CreateInstance(obj);
    }

    /// <summary>
    /// Converts the object value into the Typed variant of this <see cref="Value"/>
    /// </summary>
    protected K ConvertValue<K>(object Value, Type PropertyType)
    {
        // No need to change type if types match
        if (Value.GetType() == PropertyType)
            return (K)Value;

        // Enums need special care
        if (PropertyType.IsEnum)
            return (K)Enum.Parse(PropertyType, Value.ToString(), true);

        return (K)Convert.ChangeType(Value, PropertyType);
    }

    /// <summary>
    /// Converts the object array values into the Typed variant of this <see cref="Value"/>
    /// </summary>
    protected object ConvertArray(Object[] values, Type propertyType)
    {
        // Set the value to the instanced object
        switch (propertyType.Name.ToLowerInvariant())
        {
            case "int[]":
            case "int32[]":
                return Array.ConvertAll(values, Convert.ToInt32);
            case "string[]":
                return Array.ConvertAll(values, Convert.ToString);
            case "double[]":
                return Array.ConvertAll(values, Convert.ToDouble);
            default:
                throw new Exception("Invalid property type: " + propertyType.Name);
        }
    }
}

感谢您查看我的问题,希望有人可以帮我解决这个问题。

编辑:调用方法

public class Example
{
    [PropertyName("isExample")]
    public ObjectProperty<bool> IsExample;

    // .....

    public void Parse(string[] Params)
    {
        // Fetch our property that we are setting the value to
        FieldInfo prop = this.GetType().GetField("isExample");

        // Create our instance, and parse
        ObjectPropertyBase obj = ObjectPropertyBase.Create(prop);
        obj.SetValueFromParams(Params);
        prop.SetValue(this, obj);
    }
}    
    // Base Class

    public abstract class ObjectPropertyBase
    {
        /// <summary>
        /// Takes an array of string values, and converts it to the proper value type for
        /// this instance's Generic Type
        /// </summary>
        /// <param name="ValueParams">The string value's to convert, and set the 
        /// Value of this instance to.
        /// </param>
        public abstract void SetValueFromParams(String[] value);

        /// <summary>
        ///     Using reflection, this method creates a new instance of the <paramref name="field"/>
        ///     type, and returns it.
        /// </summary>
        /// <remarks>
        ///     This method does NOT set the field value, since hey... we dont have an instance to work on
        /// </remarks>
        /// <param name="field">The field we are creating an instance for. </param>
        /// <exception cref="System.Exception">
        ///     Thrown if the Field provided does not contain the <see cref="PropertyName"/> attribute
        /// </exception>
        public static ObjectPropertyBase Create(FieldInfo field)
        {
            // If the Custom attribute exists, we add it to the Mapping
            Attribute attribute = Attribute.GetCustomAttribute(field, typeof(PropertyName));
            if (attribute == null)
                throw new Exception($"Internal property \"{field.Name}\" does not contain a PropertyName attribute!");

            // Get our constructor
            PropertyName fieldAttr = attribute as PropertyName;
            return (ObjectPropertyBase)Activator.CreateInstance(
                field.FieldType,
                new object[] { }
            );
        }
    }

示例:

1 个答案:

答案 0 :(得分:0)

我弄清楚我做错了什么..我的反射代码没有任何问题,而是调用&#34; SetValueFromParams&#34;方法。在用新实例覆盖它之前,我首先检查C#对象的ObjectProperty字段是否为空(以及新的List)。

修复:

public void Parse(string[] Params)
{
    // Fetch our property that we are setting the value to
    FieldInfo prop = this.GetType().GetField("IsExample");
    ObjectPropertyBase obj;

    // Get the value that is set
    var value = prop.Value.GetValue(this);

    // If the value is null, then we create a new object property instance
    if (value == null)
        obj = ObjectPropertyBase.Create(prop.Value);
    else
        obj = (ObjectPropertyBase)value;

    // Set Values
    obj.SetValueFromParams(Params);
    prop.SetValue(this, obj);
}

感谢所有花时间看我问题的人!