去掉泛型类型参数:有可能吗
本文关键字:有可能 泛型类型参数 | 更新日期: 2024-09-12 14:38:15
考虑这个类似DAL的代码:
mDealerTypes = Read<Dictionary<string, DealerType>, DealerType>(
() => { return DALFactory.Instance.ReadDealerTypes(cn); },
(c, o) => c.Add(o.DealerTypeKey, o));
mCountries = Read<Dictionary<string, Country>, Country>(
() => { return DALFactory.Instance.ReadDealerTypes(cn); },
(c, o) => c.Add(o.Code, o));
private C Read<C, T>(Func<SqlConnection, IDataReader> func, Action<C, T> a)
where C : ICollection, new()
where T : EntityBase, new()
{
C objects = new C();
using (IDataReader reader = func(connection))
{
while (reader.Read())
{
T obj = new T();
obj.Init(reader);
a(objects, obj);
}
}
return objects;
}
为了可读性,我想以某种方式更改它,这样存储在集合中的对象就不会在Read<>的类型参数列表中重复两次。
mCountries = Read<Dictionary<string, Country>>(
() => { return DALFactory.Instance.ReadDealerTypes(cn); },
(c, o) => c.Add(o.Code, o));
但是如何重写Read<>然后这可能吗?
私人??读取(功能,动作a)
创建一个简单的包装器方法(或扩展方法)
public Dictionary<T1, T2> ReadEx<T1, T2>(...) where T2 : EntityBase
{
return Read<Dictionary<T1, T2>, T2>(...)
}