0

我想在实体框架中获取表数据模型的属性名称。我有这个代码

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

 }

是否可以?提前感谢

4

1 回答 1

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);
于 2013-04-03T08:57:37.210 回答