mardi 16 mars 2021

Can I get PropertyInfo for a field or property in a class without using reflection?

I have tried searching around using Expressions but not able to find something by which I can access the fields or properties of a class without using reflection.

Basically, I will get a string at runtime, and I know that that string will be a property of the class, but I need to validate that it indeed is a property inside that class.

e.g. If I have a class:

class Test { string a; public string b {get;set;} }

I get the string values a and b at runtime and I need to verify that they exist inside the class Test

What I know till now from researching is that I can do:

string one = "a";
string two = "b";
PropertyInfo result1 = typeof(Test).GetProperty(one);
PropertyInfo result2 = typeof(Test).GetProperty(two);

But this code is using reflection. I want to know if there is some way I can do this without using reflection?

Can I do this using Expressions?





Aucun commentaire:

Enregistrer un commentaire