我想在实体框架中获取表数据模型的属性名称。 我有这个代码
var properties = context.GetType().GetProperties(BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
我想要的是将此代码包含在类似下面的方法中,以便拥有一个定义表模型的字符串变量:
MyMethod (string category)
{
var properties = "category".GetType().GetProperties(BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
......
......
}
有可能吗? Thanx提前
答案 0 :(得分:1)
您可以使用Assembly.GetType(string)方法执行此操作。您的代码看起来像这样:
// Don't forget to null-check the result of GetType before using it!
// You will also need to specify the correct assembly. I've assumed
// that MyClass is defined in the current executing assembly.
var properties = Assembly.GetExecutingAssembly().GetType("My.NameSpace.MyClass").
GetProperties(
BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
您也可以使用Type.GetType(string)
var properties = Type.GetType("My.NameSpace.MyClass").
GetProperties(
BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);