我希望能够在泛型方法中使用词典。我正在寻找的是一种从字典的Key和Value获取类型的方法发送到通用扩展方法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));
}
目标是返回正确输入的字典
答案 0 :(得分: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")
答案 1 :(得分: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;
}
答案 2 :(得分:0)
为什么不直接定义你的方法:
public static IDictionary<K, V> LoadProperty<K, V>(this EntityObject entity, string name)
答案 3 :(得分: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);
}
虽然这在调用时会更加冗长。