Mike_R Posted July 5, 2006 Posted July 5, 2006 (edited) I was wondering if anyone had some thoughts on assigning a Delegate to a Property, as defining a "Property Delegate" does not seem to be allowed in VB.NET. However, since the underlying accessor methods are really just get_Property() and set_Property(value) methods, this restriction seems odd. In VB.NET, the accessor methods do not appear to be publicly exposed. Are they in C#? (I'm guessing no?) I guess I could create a Public Get and Set method pair and have those methods called by the respective Property Get and Property Set functions. But since the methods would have to be public (if I wish to assign delegates to them) it is then redundant to also have a public property doing exactly the same thing -- so I may as well throw out the property altogether and just stick with a Get and Set method pair only. I could use an interface that includes a property and then pass in a reference to the class that implements the property. This is fine, I guess, but not as flexible as a delegate, unless one is willing to make a lot of pre-defined interfaces with different property return types. A final thought is that I could use Reflection to find the property's accessor methods and then create an instance of the PropertyInfo class, which would effectively become my *Delegate*. A little ugly, but doable. Funny that I could create a "surrogate delegate" via a PropertyInfo class or via a class+interface, but directly using a Delegate class appears to be off limits to Properties... Does anyone have any thoughts or ideas on this? Mike Edited July 6, 2006 by Mike_R Quote Posting Guidelines Avatar by Lebb
Administrators PlausiblyDamp Posted July 6, 2006 Administrators Posted July 6, 2006 If you want to have a property that has a data type of delegate then Public Property TestProp() As [Delegate] Get End Get Set(ByVal Value As [Delegate]) End Set End Property should work. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Gill Bates Posted July 6, 2006 Posted July 6, 2006 delegates exposed as properties in C# are usually events. But you also expose a public property that is a delegate. Here's both:public class MyClass { public delegate void MyDelegate(); private MyDelegate _delegateProperty; public MyDelegate DelegateProperty { get { return _delegateProperty; } set { _delegateProperty = value; } } public event MyDelegate SomeEvent { add { _delegateProperty += value; } remove { _delegateProperty -= value; } } } Quote
Mike_R Posted July 6, 2006 Author Posted July 6, 2006 Ok, thanks guys, I appreciate it... But that wasn't quite what I was looking for. I'm sorry, I guess I was not clear on what I meant. In VB.NET (and I would assume in C# as well) a delegate cannot be defined as a Property. Only a Method can be defined as part of a delegate's signature. This means that in VB.NET a Delegate class can be defined for a Sub or a Function, but not for a Property. For example, the following is permitted in VB.NET:Delegate Function MyFunction As String while the following is not legal: Delegate ReadOnly Property MyFunction As String This would seem a bit of an odd restriction given that a read only Property and a parameterless Function are essentially the same. But I guess in general, the problem with having a Delegate that defines a Property is that most properties are Read-Write and so defining a Delegate to it would be misleading as one would actually need TWO delegates: one for 'Get' and the other for 'Set'. So restricting Delegate definitions to Methods-only really does makes sense after all... Thanks guys, Mike Quote Posting Guidelines Avatar by Lebb
Administrators PlausiblyDamp Posted July 6, 2006 Administrators Posted July 6, 2006 Ahhh, get what you mean. As far as I am aware delegates cannot be defined as a property, this could be due to the fact that not all languages support properties (but I'm guessing), or the fact that a property doesn't seem to be a method call. If you require the multicast functionality of delegates then you are probably out of luck, if it is just as a method signature then you could always define an interface with a single property defined instead. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mskeel Posted July 6, 2006 Posted July 6, 2006 Mike_R, you sure manage to find the really interesting stuff. Very cool find. It might be worth noting that the names of the properties change in the compiled IL. When you reflect on a property, two methods are indeed generated for a general property: "get_MyProperty" and "set_MyProperty". So I suppose it does make it sort of difficult to get the address of something that doesn't yet have a permanent name. Quote
Leaders snarfblam Posted July 6, 2006 Leaders Posted July 6, 2006 I second that, mskeel. Interesting point indeed. It is unfortunate that Microsoft felt the need to completely hide the accessors from you without providing some kind of complementary feature to provide a delegate-like operation with a property. How about something like: delegate get int myGetAccessor(); void stuff() { myGetAccessor g = (get)this.SomeIntProperty; } Delegate Property Get MyGetAccessor() As Integer Sub Stuff() Dim g As MyGetAccessor = AddressOf Me.SomeIntProperty End Sub Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.