How can I refer to the properties of an object of type Employee, for example, dynamically? I'm after something like employee."hasBeenPaid"? Does it involve reflection?
class Employee { String name; Bool hasBeenPaid; } How can I refer to the properties of an object of type Employee, for example, dynamically? I'm after something like employee."hasBeenPaid"? Does it involve reflection?
class Employee { String name; Bool hasBeenPaid; } You could try:
Type type = your_class.GetType(); PropertyInfo propinfo = type.GetProperty("hasBeenPaid"); If you need the value
value = propinfo.GetValue(your_class, null); You may use the dynamic C# feature; and yes, it will use reflection at runtime to resolve your properties.
dynamic in it's basic form isn't enough to cover these requirements, use of a string also implies the ability to provide a variable instead of a literla. dynamic without any changes uses duck-typing, if you want to use strings you need to do something like this: stackoverflow.com/questions/2783502/… And it doesn't work against his provided entity and assumes public member access.