1

我希望能够在通用方法中使用字典。我正在寻找一种从发送到通用扩展方法 LoadProperty 的字典的键和值中获取类型的方法。

这是我到目前为止所做的。

我将该方法称为扩展

entityObject.LoadProperty<Dictionary<string, int>>("CartonThreshold")
//entityObject.LoadProperty<Dictionary<string, double>>("CartonThreshold")

// ... 

public static T LoadProperty<T>(this EntityObject entity, string name) where T : new()
{
    EntityObjectProperty prop = entity.Properties.Single(x => x.Name.Equals(name));

    // If request dictionary
    if (typeof(T).GetInterface(typeof(IDictionary<,>).Name) != null || typeof(T).Name.Contains("IDictionary"))
    {
        var dictionaryInstance = (IDictionary)new T();

        // Just for testing
        var a = dictionaryInstance.Keys.GetType().Name;
        var b = dictionaryInstance.Values.GetType().Name;

        var key = (T) Convert.ChangeType(prop.Name, typeof (T));
        var value = (T) Convert.ChangeType(prop.Value, typeof (T));
        dictionaryInstance.Add(key, value);
        return (T) dictionaryInstance;
    }

    // default
    return (T)Convert.ChangeType(prop.Value, typeof(T));
}

目标是返回正确类型的字典

4

4 回答 4

1

我正在寻找的是 GetType() 的 GetGenericArguments() 扩展

下面的方法将返回我需要的东西。

public static T LoadProperty<T>(this EntityObject entity, string name) where T : new()
{
    EntityObjectProperty prop = entity.Properties.Single(x => x.Name.Equals(name));

    try
    {
        // If request dictionary
        if (typeof(T).GetInterface(typeof(IDictionary<,>).Name) != null || typeof(T).Name.Contains("IDictionary"))
        {
            var dictionaryInstance = (IDictionary)new T();

            var typing = dictionaryInstance.GetType().GetGenericArguments();
            Type keyType = typing[0];
            Type valueType = typing[1];

            // dictionary fallback, set to default of the valuetype if null
            object value = prop.Value != null ? Convert.ChangeType(prop.Value, valueType) : Activator.CreateInstance(valueType);
            var key = Convert.ChangeType(prop.Name, keyType);
            dictionaryInstance.Add(key, value);
            return (T)dictionaryInstance;
        }

        if (prop.Value != null)
        {
            // default
            return (T)Convert.ChangeType(prop.Value, typeof(T));
        }
    }
    catch { }
    return default(T);
}

这样我可以像这样调用方法

// Will return a typed dictionary with the EntityObjectProperty name as key and the EntityObjectProperty Value as value
entityObject.LoadProperty<Dictionary<string, int>>("CartonThreshold")

// Will return a string of the EntityObjectProperty Value
entityObject.LoadProperty<string>("CartonThreshold")

// Will return an Int of the EntityObjectProperty Value
entityObject.LoadProperty<int>("CartonThreshold")
于 2012-07-06T13:15:55.367 回答
0

你为什么不像这样定义你的方法:

public static IDictionary<K, V> LoadProperty<K, V>(this EntityObject entity, string name)
于 2012-07-06T13:15:48.250 回答
0

如果我正确理解了您的问题,您想要做的是这样定义您的方法:

public static T LoadProperty<T,TKey,TValue>(this EntityObject entity, string name) 
    where T : IDictionary<TKey,TValue>, new() {
    EntityObjectProperty prop = entity.Properties.Single(x => x.Name.Equals(name));
    T t = new T();
    t.Add((TKey)prop.Name, TValue(prop.Value));
    return t;
}

然后调用它

Dictionary<string,int> property = entityObject.LoadProperty<Dictionary<string,int>, string, int>("test");

坏事是你每次都必须指定所有的通用参数......
如果你的具体情况没问题,它会像这样简单得多:

public static IDictionary<TKey,TValue> LoadProperty<TKey,TValue>(this EntityObject entity, string name) {
    EntityObjectProperty prop = entity.Properties.Single(x => x.Name.Equals(name));
    var dict = new Dictionary<TKey,TValue>();
    dict.Add((TKey)prop.Name, TValue(prop.Value));
    return dict;
}

编辑:在您澄清之后,如何通过通用参数的数量来区分这两种情况?

public static IDictionary<TKey,TValue> LoadProperty<TKey,TValue>(this EntityObject entity, string name) {
    var d = new Dictionary<TKey,TValue>();
    // add value from EntityObject 
    return t;
}

public static TValue LoadProperty<TValue>(this EntityObject entity, string name) {
    TValue ret;
    // get value from EntityObject 
    return ret;
}
于 2012-07-06T13:11:28.733 回答
0

为了让它与几乎任何东西一起工作,你可以传递一个函数来创建你想要的任何返回类型:

public static T LoadProperty<TKey,TValue,T>(this EntityObject entity, string name,  Func<TKey,TValue,T> createWhatever) 
{
    EntityObjectProperty prop = entity.Properties.Single(x => x.Name.Equals(name));
    return createWhatever((TKey)prop.Name, (TValue)prop.Value);
}

不过,这在调用时会更加冗长。

于 2012-07-06T13:29:26.213 回答