g bas
g bas

Reputation: 808

get the name of properties of a table using string

I want to get the name of properties of a table-data model in entity framework. I have this code

 var properties = context.GetType().GetProperties(BindingFlags.DeclaredOnly |
                                        BindingFlags.Public |
                                        BindingFlags.Instance);

All i want is to include this code in a method like the one following in order to have a string variable that defines the table-model:

 MyMethod (string category)
 {
     var properties = "category".GetType().GetProperties(BindingFlags.DeclaredOnly |
                                    BindingFlags.Public |
                                    BindingFlags.Instance);
   ......
   ......

 }

Is it possible? Thanx in advance

Upvotes: 0

Views: 204

Answers (1)

RB.
RB.

Reputation: 37232

You can use the Assembly.GetType(string) method to do this. Your code would look something like this:

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

You can also use Type.GetType(string)

var properties = Type.GetType("My.NameSpace.MyClass").
    GetProperties(
        BindingFlags.DeclaredOnly |
        BindingFlags.Public |
        BindingFlags.Instance);

Upvotes: 1

Related Questions