For a given property, the get method is named "get_" followed by the
property name, and the set method is named "set_" followed by the
property name. Using Delegate.CreateDelegate(), one can then synthesize
a delegate against the property get or set method.

Eg:

System.Delegate.CreateDelegate(typeof(delegateType),target,"get_MyProp")
;

The main issue you're going to have is how much type information do you
have at compile time. Delegates are types, and to create a delegate
instance, one must specify the delegate type. So for a property of type
string, one would need two delegates:

public delegate void StringPropertySet(string value);
public delegate string StringPropertyGet();

To create a delegate for the Foo property on an instance of the bar
class one would do something like:
`
(StringPropertySet)System.Delegate.CreateDelegate(
       typeof(StringPropertySet),barInstance,"set_Foo");

So, if you have a limited number of types for your properties, you can
define all the delegates up front, and use them at will. If you don't
know the properties data type at compile time, you'll have to define the
delegate somehow, probably using reflection.emit. You could also pass
around instance of the property's PropertyInfo, and a reference to the
target, then just use the Ppropertyinfo.SetValue and GetValue().

HTH
-Ethan

-----Original Message-----
From: dotnet discussion [mailto:[EMAIL PROTECTED]] On Behalf Of
Martin Wawrusch
Sent: Saturday, April 13, 2002 10:49 AM
To: [EMAIL PROTECTED]
Subject: [DOTNET] Delegates referencing a Property

Hi,

I would like to reference a property get or set operation with a
delegate, however I find no information about the syntax in the docs. Is
this possible, and if so, how?

Thanks

Martin


--
Neovalis IT Development GmbH  T: +43 1 522 68 68
Neustiftgasse 17/6            F: +43 1 522 68 68 - 33
1070 Wien                     W: www.neovalis.cc
                              M: [EMAIL PROTECTED]

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to