List, select and retrieve properties
am 09.10.2007 21:29:06 von unknown
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 Sosa Albert
Re: List, select and retrieve properties
am 09.10.2007 22:06:14 von 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
"Carlos Sosa Albert" wrote in message
news:E257EBEA-5920-4C44-8857-78F97260D3FC@microsoft.com...
> 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 Sosa Albert
Re: List, select and retrieve properties
am 09.10.2007 22:10:28 von mattias.dont.want.spam
>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
--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.