List, select and retrieve properties

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi guys,

Using C# I have a class with a number of public properties and a public
method.
The idea is to call the method (externally), selecting one of those
properties. Do some work with the property, and then return a value.
Something like this:

public string MyMethod(MyClass.MyProperties selectedProperty)
{
// do some work
return "bla, bla, bla" + _SelectedPropertyValue;
}

Is it possible? I can't seem to find some way to "list" the properties or to
retrieve the property value "dynamically".

Thanks a lot!
 
Carlos,

To retrieve a property you need to use reflection

PropertyInfo info = instance.GetType().GetProperty("MyProperty")
if (info != null)
object value = info.GetValue(instance, null);

- or -

info.SetValue(instance, newvalue, null);


Mark
 
Is it possible? I can't seem to find some way to "list" the properties or to
retrieve the property value "dynamically".

Yes it's possible using Reflection. Check out
System.Type.GetProperties().


Mattias
 
Back
Top