Is it possible to cast from a Dynamic or an Object to a variable type? The target variable type is one generic class, only the specific data type varies.
I'm looking for abstract solution, but the situation is this: I want a single Linq2SQL command for different Entities - I can GetProperty + GetValue of the EntityFramework Context, but then it must be casted to some exact types, but these come from variable input - names of DB tables.
Example of what I have:
Type NewType = typeof(System.Data.Objects.ObjectSet<>);
NewType.MakeGenericType(new Type[] {"...some type here that's variable accordint to user input - like Person, Address, Contact, ..."});
Object MyObject = Context.GetType().GetProperty("the same variable like above - Person, Address,...", BindingFlags.Public | BindingFlags.Instance).GetValue(Context, null); // this is EntityFramework Context
Example of what I need but don't know how to do it:
NewType CastedObject = (NewType) MyObject;
// or
NewType CastedObject = Convert.ChangeType(MyObject, NewType);
// or
NewType CastedObject = MyObject as NewType;
so I could do this:
(from x in CastedObject where x.ID == ID select x).FirstOrDefault();
For those, who are familiar with PHP, I'm trying to do this:
(from x in Context.$tablename where x.ID == ID select x).FirstOrDefault();