Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / March 2005

Tip: Looking for answers? Try searching our database.

how to get attribute initialization data through reflection?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
maxx - 30 Mar 2005 04:59 GMT
In language, we apply an attribute to a type or member by specifying a
constructor (of that attribute type) and passing appropriate arguments. With
reflection, we can get the attribute instances through
"MemberInfo.GetCustomAttributes()". But this way, we can only get the
instances, not how they are initialized, ie. which constructor is used and
what arguments are passed. This information is written in the metadata and
both Reflector and ILDASM can display it. I am wondering if there is some
.net library method that can get this information, or I'll have to inspect
the raw metadata?

The following example illustrate what I am talking about.

public class MyAttrAttribute: Attribute
{
   int _i;
   string _s;
   public MyAttrAttribute(int i, string s) {}
   public MyAttrAttribute(string s, int i) { _i=i; _s=s;}
}

[MyAttr(3+5, "good"+"bye")]
public class MyClass { }

Reflector display the decompiled MyClass
[MyAttr(8, "goodbye")]
public class MyClass
{
     // Methods
     public MyClass();
}

and ILDASM shows
.class public auto ansi beforefieldinit MyClass
      extends [mscorlib]System.Object
{
 .custom instance void ConApp.MyAttrAttribute::.ctor(int32, string) =
         ( 01 00 08 00 00 00 07 67 6F 6F 64 62 79 65 00 00 ) //
.......goodbye..
} // end of class MyClass
john conwell - 30 Mar 2005 17:37 GMT
I've used custom attributes fairly extensivly and havent seen a way to do
what you want.  What i'd suggest is to look at the problem a different way.  
If you have two different constructors, then the internal data will probably
be different for two instances of the attribute that were created by
different constructors.  So just look at the internal state (exposed via
public properties of course) to determine how the attribute was instanciated.
Or just expose one public member that evaulates the internal state of the
attribute and returns a flag that tells the caller how it was created.

> In language, we apply an attribute to a type or member by specifying a
> constructor (of that attribute type) and passing appropriate arguments. With
[quoted text clipped - 35 lines]
> .......goodbye..
> } // end of class MyClass
maxx - 30 Mar 2005 19:01 GMT
Thanks for your response. On an individual basis, it's sometimes possible to
deduce how an attribute instance was constructed by inspecting the interal
state, but I need a univeral and programmatic way to accurrately retrieve
this information for any attributes. I agree with you that there's probably
no way to do this through Reflecton. Guess I'll have to jump into the
unmanaged world now.

> I've used custom attributes fairly extensivly and havent seen a way to do
> what you want.  What i'd suggest is to look at the problem a different way.  
[quoted text clipped - 44 lines]
> > .......goodbye..
> > } // end of class MyClass
john conwell - 30 Mar 2005 19:39 GMT
So what specifically are you trying to solve?  How will using unmanaged code
give you anything more than managed?

> Thanks for your response. On an individual basis, it's sometimes possible to
> deduce how an attribute instance was constructed by inspecting the interal
[quoted text clipped - 51 lines]
> > > .......goodbye..
> > > } // end of class MyClass
maxx - 30 Mar 2005 20:47 GMT
well...I am writing a tool to display type information in an assembly. It's
more friendly to present the attributes in the original form that a user
would have written in code than throw a dump of internal state. As I said,
the information is in the metadata of the PE file and Microsft seems to have
provided some unmanaged API to retrive raw information in metadata.

> So what specifically are you trying to solve?  How will using unmanaged code
> give you anything more than managed?
[quoted text clipped - 54 lines]
> > > > .......goodbye..
> > > > } // end of class MyClass

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.