Finding the object instance from the attribute instance

  • Thread starter Thread starter tobiwan_kenobi
  • Start date Start date
T

tobiwan_kenobi

public static InfoAttribute GetInfo(Enum instance)
{
Type type = instance.GetType();
MemberInfo[] mis = type.GetMember(instance.ToString(), BindingFlags.Public
|
BindingFlags.NonPublic | BindingFlags.Static |
BindingFlags.FlattenHierarchy);

Type attributeType = typeof(InfoAttribute);
IList attrs = FindAttributes(false, memberInfo, attributeType);
InfoAttribute ia = attrs[0] as InfoAttribute;
return ia;
}

Now that I have a reference to an instance of the attribute for an object.
How can I get the instance of the object to which the instance of the
attribute is associated?

public static object GetInfo(InfoAttribute attribute)
{
...
return infoObject
}
 
You can't.

First the attribute isn't specific to the instance, and second there
still is no way (that I know of) of tracing back from an attribute to
the declaring member.

Marc
 
Marc Gravell said:
You can't.

First the attribute isn't specific to the instance,

You're right. The reason I was thinking that they did is because I was
looking at attributes associated with each member of an enumeration. But
yes, you are right. An attribute is attached to the class (or to the
static?).
and second there
still is no way (that I know of) of tracing back from an attribute to
the declaring member.

Hmm. Then, I'm still looking.
 
Back
Top