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