Ron*_*ald 7 asp.net entity-framework
(注意,下面的代码只是示例.请不要评论为什么这是必要的.我希望肯定答案是或否,如果有可能那么如何?如果没有它也没关系.如果问题很模糊也让我知道.谢谢!)
例如,我可以在下面获得ObjectSet < T >:
ObjectSet<Users> userSet = dbContext.CreateObjectSet<Users>();
ObjectSet<Categories> categorySet = dbContext.CreateObjectSet<Categories>();
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常.但是,我需要实体表是动态的,所以我可以在类型之间切换.像下面的东西.
//var type = typeof(Users);
var type = typeof(Categories);
Object<type> objectSet = dbContext.CreateObjectSet<type>();
Run Code Online (Sandbox Code Playgroud)
但上面的代码将无法编译.
[编辑:]我想要的是类似的东西,或类似的东西:
//string tableName = "Users";
string tableName = "Categories";
ObjectSet objectSet = dbContext.GetObjectSetByTableName(tablename);
Run Code Online (Sandbox Code Playgroud)
小智 6
我已经通过以下调整对上述建议进行了调整:
var type = Type.GetType(myTypeName);
var method = _ctx.GetType().GetMethod("CreateObjectSet", Type.EmptyTypes);
var generic = method.MakeGenericMethod(type);
dynamic objectSet = generic.Invoke(_ctx, null);
Run Code Online (Sandbox Code Playgroud)
您可以使用“如何使用反射来调用泛型方法”中的示例吗?
var type = typeof(Categories); // or Type.GetType("Categories") if you have a string
var method = dbContext.GetType.GetMethod("CreateObjectSet");
var generic = method.MakeGenericMethod(type);
generic.Invoke(dbContext, null);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5895 次 |
最近记录: |