【问题标题】:How to instantiate a collection of type MyCollection<T> : IList<Alert<T>>?如何实例化 MyCollection<T> 类型的集合:IList<Alert<T>>?
【发布时间】:2011-01-13 10:38:45
【问题描述】:

我有一个Alert &lt;T&gt; 对象。 假设我想获取 MyObject 类型的所有警报, 我会有一个类型的集合 MyCollection&lt;MyObject&gt; : IList&lt;Alert&lt;MyObject&gt;&gt;.

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

【问题讨论】:

  • 如果你想隐藏 Alert,为什么要从 IList 派生而不是具体实例,例如: public class MyCollection : List>{ }

标签: c# generics collections c#-4.0


【解决方案1】:

我想我找到了我正在寻找的重要部分:

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

从数据上下文中获取实体的通用方法

【讨论】:

    【解决方案2】:

    我想我找到了解决方案,虽然我还没有机会测试它。

    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

    【讨论】:

    • 这似乎是一个很好的解决方案,我将所有类型都放入 Base.. 这似乎可以节省我编写大量代码.. 有没有人使用过这种类型的解决方案?你怎么看?我会感谢你的 cmets。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 重要的是要有一个通用类型警报。就列表而言,我不确定最好的解决方案是什么,AFAIK 可能是 'List> >'.. 我的问题是如何从 'var allobjects = dbcontext.????' 创建一个 'Alert>' 列表更基本。
      • 嗨,Benny,看来我必须重新审视我实现警报对象的方式,而且由于它容易受到频繁更改的影响,泛型可能不是最好的方法。所以代替 Alert Id 去 class Alert { public AlertObject Item {get;set;} //type enum public int ObjectID {get;set;} 谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多