2010-02-05 50 views
0

我有一个Alert <T>对象。 假设我想类型为MyObject, 我会 MyCollection<MyObject> : IList<Alert<MyObject>>类型的集合中的所有警报。如何实例化MyCollection类型的集合<T>:IList <Alert<T>>?

我该如何实现该列表的方法?

+0

得到实体如果你想隐藏的通用方法警告,为什么从IList派生而不是已经具体的实例,如:public class MyCollection :List > {} – 2010-02-23 12:21:26

回答

1

让我先问你,为什么建立自己的自定义集合?你真的需要它吗?如果是这样的话,您可能需要查看MSDN herehere,如果不使用框架中已有的任何通用集合类。

+0

重要的是要有一个通用类型警报.. 就列表而言,我不知道什么是最佳解决方案,AFAIK它可能是'列表<警报 >>'.. 我的问题虽然对于如何从'var allobjects = dbcontext'创建'Alert >'列表更为基础。'' – safhac 2010-02-05 09:44:15

+0

喜尼, 看来我必须重新审视我实现我的提醒对象的方式,因为它容易受到频繁的变化仿制药可能不是去它的最好方式。 所以不是警报的 标识去 级警报{ 公共AlertObject项目{获得;设置;} //枚举类型 公众诠释对象ID {获取;集;} 感谢 – safhac 2010-02-11 07:28:16

+0

很高兴你找到了解决办法! – BennyM 2010-02-11 08:48:27

0

我想的解决方案应该是某种动态构造的..

0

我想我找到了解决办法,虽然我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); 
     } 
    } 


} 

by Joanna Carter's c# version of Delphis Meta Class

+0

这似乎是一个很好的解决方案,我得到的所有类型分为基地。 看起来这将节省我写了很多的代码.. 有没有人使用过这种类型的解决方案?你怎么看? 我会感谢您的意见。 – safhac 2010-02-08 12:27:11

0

我想我找到什么即时通讯的一个重要棋子寻找:

entityObject = objectContext.GetEntityByKey<T>(id); 

从DataContext的

相关问题