I have a method
public void Insert<T>(T o) where T : class
{
c.Set<T>().Add(o);
}
I need to use it with an object type
object x = ....
r.Insert(x);
but since it’s an object T == object, but I need it to be the x.GetType() type
anybody knows how to do this ?
You’d basically have to call it via reflection (e.g. using
MethodInfo.MakeGenericMethod). Generics is about providing compile-time type safety, whereas you don’t know the type at compile time. Alternatively you can use dynamic typing if you’re on C# 4.Using reflection is a pain in terms of:
If you’re using C# 4, the dynamic typing solution would be: