accessing a properties PropertyInfo  
Author Message
PhilipDaniels





PostPosted: .NET Base Class Library, accessing a properties PropertyInfo Top

I want to access a property's PropertyInfo without knowing it's name, I can do this if an accessor invokes another method via putting the following code in the invoked method.

string caller = new StackTrace.GetFrame(1).GetMethod().Name;
string pName = caller.Name.SubString(caller.Name.IndexOf('_'));
PropertyInfo info = <this>.GetType().GetProperty(pName);

As a result the same method can be readily invoked by accessors of different properties and the method can do its work based on the property name, type, custom attributes etc

But I also want to do something similar in an associated method that is not invoked by an accessor - but by a method in another class, which has an instance of the properties declaring class.

If I were talking about an event I could implement a .other method which would have the name info_<eventName> and have it return the EventInfo via the following code

MethodBase thisMethod = MethodBase.GetCurrentMethod();
string pName = thisMethod.Name.SubString(thisMethod.Name.IndexOf('_'));
PropertyInfo info = this.GetType().GetProperty(pName);
return info;

Then I could get the EventInfo by invoking info_<eventName>.

But there doesn't appear to be anything like OtherMethods for properties, or is it hidden somewhere that I've yet to stumble on.

I hope this makes sense

Rgds PhilD



.NET Development10  
 
 
Mattias Sjogren





PostPosted: .NET Base Class Library, accessing a properties PropertyInfo Top



Then I could get the EventInfo by invoking info_<eventName>.

How could you do that if you don't know the event name



 
 
nobugz





PostPosted: .NET Base Class Library, accessing a properties PropertyInfo Top

Weird stuff, I wonder what you're trying to accomplish. You are relying on the fact that the compiler generates an invisible name for the getter/setter of a property that happens to have the property name embedded in the method name (like get_Property). That just falls flat on its face when you try to do this by calling this code from an event handler or any other method that is not a getter or setter of a property. The name of the event handler or method just doesn't have anything to do with a property.

If you're trying to find EventInfo from an event handler method, you'd first have the force programmer to choose a name for the handler that is compatible with your approach. That still doesn't get you anywhere though, the delegate type and the MulticastDelegate instance is declared in some other class, one you can't find back from the either the handler name or the class than contains the handler.


 
 
PhilipDaniels





PostPosted: .NET Base Class Library, accessing a properties PropertyInfo Top

What I want is to do is to detect when a property gets a new value. My introduction of events into my original post rather confused the issue, the reason I did is that events can be extended by additional methods, I was lamenting the fact that properties are not similarly endowed.

In one case I have a set of 9 buttons that I want to enable/disable depending on the values of 4 variables (properties). The easiest way is to monitor the 4 variables via their properties using a common method that enables the buttons according to a truth table based on the 4 property values.

There are two points at which I must "identify" the property

1. when I 'attach' the method (the one with the truth table) to the properties in question - this is done via a 'monitor' class whose base is

Dictionary<object.hashvalue, Dictionary<string, List<MyDelegate>>>

2. when the property set accessors 'notify' the monitor of their new value

There are two ways to identify a member, by its name (a string) or via its metadata token (an int and hence more efficient).

Sure I know the property names but the 'monitor' needs to be told the property names. I've devised a means via which the source of a properties identity is the same in both the 'attach' and and 'notify' contexts; it is not hard coded but it is derived from the get accessors name. I'm looking into replacing the name as an identifier with the metadata token for efficiency reasons (int compares being faster than string compares), but the get accessor name will still be the source.

I find it odd that a method can access its own MethodBase, but other member types cannot get at their equivalents, a property accessor can get its MethodBase but not its PropertyInfo, an event accessor can access its MethodBase but not its EventInfo - it all seems a bit arbitrary to me.