List, select and retrieve properties

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!
 
M

Mark Dykun

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
 
M

Mattias Sjögren

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top