views:

232

answers:

4

I have an Alert <T> object. suppose I want to get all the alerts for type MyObject, I would have a collection of type MyCollection<MyObject> : IList<Alert<MyObject>>.

How would I implement methods for that list?

+1  A: 

Let me first ask you, why build your own custom collection? Do you really need it? If so you might want to take a look at MSDN here and here, if not use any of the generic collection classes that are already in the framework.

BennyM
The important thing is to have a generic type alert..As far as the list is concerned i'm not that sure whats the best solution, AFAIK it could be 'List<Alert<MyObject>>>'..My question though is more basic to how to create a list of 'Alert<MyObject>>' from 'var allobjects = dbcontext.????'
CohenA
Hi Benny,It seems I had to review the way I implemented my alert object, And since it is susceptible to frequent changes Generics may not be the best way to go about it. So instead of Alert<T>Id go for class Alert {public AlertObject Item {get;set;} //type enumpublic int ObjectID {get;set;}Thanks
CohenA
Glad you found a solution!
BennyM
A: 

I suppose the solution should be some sort of dynamic constructor..

CohenA
A: 

I think I found a solution though I hav'nt had a chance to test it yet.

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

CohenA
This seems like a good solution and I get all the types into Base..It seems like this will save me writing a lot of code.. Has anyone ever used this type of solution? what do you think?I will appreciate your comments.
CohenA
A: 

I think I found an important piece of what im looking for:

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

a generic method for getting the entities from the datacontext

CohenA