我有一个Alert <T>
对象。 假设我想类型为MyObject, 我会 MyCollection<MyObject> : IList<Alert<MyObject>>
类型的集合中的所有警报。如何实例化MyCollection类型的集合<T>:IList <Alert<T>>?
我该如何实现该列表的方法?
我有一个Alert <T>
对象。 假设我想类型为MyObject, 我会 MyCollection<MyObject> : IList<Alert<MyObject>>
类型的集合中的所有警报。如何实例化MyCollection类型的集合<T>:IList <Alert<T>>?
我该如何实现该列表的方法?
重要的是要有一个通用类型警报.. 就列表而言,我不知道什么是最佳解决方案,AFAIK它可能是'列表<警报
喜尼, 看来我必须重新审视我实现我的提醒对象的方式,因为它容易受到频繁的变化仿制药可能不是去它的最好方式。 所以不是警报
很高兴你找到了解决办法! – BennyM 2010-02-11 08:48:27
我想的解决方案应该是某种动态构造的..
我想我找到了解决办法,虽然我hav'nt有机会来测试它。
public class Base
{
private delegate Base ConstructorDelegate(int someParam);
public class ClassReference
{
Type currentType = typeof(Base);
public Base Create<U>() where U : Base
{
ConstructorInfo ci = currentType.GetConstructor(BindingFlags.Instance |
BindingFlags.Public, null, Type.EmptyTypes, null);
DynamicMethod dm = new DynamicMethod("CreateInstance", typeof(Base), Type.EmptyTypes, typeof(ClassReference));
ILGenerator il = dm.GetILGenerator();
il.Emit(OpCodes.Newobj, ci);
il.Emit(OpCodes.Ret);
ConstructorDelegate del = (ConstructorDelegate)dm.CreateDelegate(typeof(ConstructorDelegate));
return del();
}
public Base Create<U>(int someParam) where U : Base
{
ConstructorInfo ci = currentType.GetConstructor(BindingFlags.Instance |
BindingFlags.Public, null, new Type[] { typeof(int) }, null);
DynamicMethod dm = new DynamicMethod("CreateInstance", typeof(Base), new Type[] {
typeof(int) }, typeof(ClassReference));
ILGenerator il = dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Newobj, ci);
il.Emit(OpCodes.Ret);
ConstructorDelegate del = (ConstructorDelegate)dm.CreateDelegate(typeof(ConstructorDelegate));
return del(someParam);
}
private ClassReference(Type type)
{
currentType = type;
}
internal ClassReference() { }
public static implicit operator ClassReference(Type input)
{
if (!typeof(Base).IsAssignableFrom(input))
throw new Exception(String.Format("Type {0} must derive from {1}", input,
typeof(Base)));
return new ClassReference(input);
}
}
}
这似乎是一个很好的解决方案,我得到的所有类型分为基地。 看起来这将节省我写了很多的代码.. 有没有人使用过这种类型的解决方案?你怎么看? 我会感谢您的意见。 – safhac 2010-02-08 12:27:11
我想我找到什么即时通讯的一个重要棋子寻找:
entityObject = objectContext.GetEntityByKey<T>(id);
从DataContext的
得到实体如果你想隐藏的通用方法警告,为什么从IList派生而不是已经具体的实例,如:public class MyCollection:List > {} –
2010-02-23 12:21:26