C# 如何实例化MyCollection类型的集合<;T>;:IList<;警惕<;T>>;?

C# 如何实例化MyCollection类型的集合<;T>;:IList<;警惕<;T>>;?,c#,generics,collections,c#-4.0,C#,Generics,Collections,C# 4.0,我有一个警报对象。 假设我想获取MyObject类型的所有警报, 我会有一个类型的集合 MyCollection:IList 我将如何实现该列表的方法?首先让我问您,为什么要构建自己的自定义集合?你真的需要它吗?如果是这样,您可能想看看MSDN,如果不使用框架中已有的任何通用集合类。我想解决方案应该是某种动态构造函数。我想我找到了一个解决方案,尽管我还没有机会测试它 public class Base { private delegate Base ConstructorDelegat

我有一个
警报
对象。 假设我想获取MyObject类型的所有警报, 我会有一个类型的集合
MyCollection:IList


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

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

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

我想我找到了一个解决方案,尽管我还没有机会测试它

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);
        }
    }


}
公共类基
{
私有委托基构造函数delegate(int-someParam);
公共类类引用
{
类型currentType=类型(基本);
public Base Create(),其中U:Base
{
ConstructorInfo ci=currentType.GetConstructor(BindingFlags.Instance|
BindingFlags.Public,null,Type.EmptyTypes,null);
DynamicMethod dm=新的DynamicMethod(“CreateInstance”、typeof(Base)、Type.EmptyTypes、typeof(ClassReference));
ILGenerator il=dm.GetILGenerator();
il.Emit(操作码.Newobj,ci);
发射(操作码Ret);
ConstructorDelegate del=(ConstructorDelegate)dm.CreateDelegate(typeof(ConstructorDelegate));
返回del();
}
公共基创建(int-someParam),其中U:Base
{
ConstructorInfo ci=currentType.GetConstructor(BindingFlags.Instance|
BindingFlags.Public,null,新类型[]{typeof(int)},null);
DynamicMethod dm=新的DynamicMethod(“CreateInstance”,typeof(Base),new Type[]{
typeof(int)},typeof(ClassReference));
ILGenerator il=dm.GetILGenerator();
il.Emit(操作码.Ldarg_0);
il.Emit(操作码.Newobj,ci);
发射(操作码Ret);
ConstructorDelegate del=(ConstructorDelegate)dm.CreateDelegate(typeof(ConstructorDelegate));
返回del(someParam);
}
私有类引用(类型)
{
currentType=类型;
}
内部类引用(){}
公共静态隐式运算符类引用(类型输入)
{
如果(!typeof(Base).IsAssignableFrom(输入))
抛出新异常(String.Format(“类型{0}必须派生自{1}”),输入,
类型(基);;
返回新的类引用(输入);
}
}
}

我想我找到了我想要的一个重要部分:

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

从datacontext获取实体的通用方法重要的是具有通用类型警报。。就列表而言,我不确定什么是最好的解决方案,可能是“列表>”。。我的问题是如何从“var allobjects=dbcontext”创建一个“Alert>”列表。嗨,Benny,看来我必须回顾一下我实现Alert对象的方式,因为它容易频繁更改,所以泛型可能不是最好的方法。因此,使用类Alert{public AlertObject Item{get;set;}//type enum public int ObjectID{get;set;}代替Alert Id谢谢这似乎是一个很好的解决方案,我把所有类型都放到了Base中。这似乎可以节省我编写大量代码。有人使用过这种类型的解决方案吗?你怎么看?我会感谢你的评论。如果你想隐藏警报,为什么要从IList派生而不是一个具体的实例,比如:public class MyCollection:列表{}