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 / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

Values of Unknown Custom Attributes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
pagates - 29 Mar 2007 15:56 GMT
Hello All,

I am sure that I am just overlooking something, but here's something I can't
quite get right...

I want to be able to get the value of a parameter of an unknown custom
attribute at runtime.  All of the examples I have seen use casting of known
custom attributes to get these values.

Thanks,
pagates
Nicholas Paldino [.NET/C# MVP] - 29 Mar 2007 16:10 GMT
pagates,

   Custom attributes are classes, they are not methods with parameters.
Getting any information from them using reflection is like using reflection
to get information from any other class.  You have to have some expectation
of what you are looking for.

   Can you provide an example of what you are trying to do?

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hello All,
>
[quoted text clipped - 9 lines]
> Thanks,
> pagates
pagates - 29 Mar 2007 16:26 GMT
Hi Nicholas,

Sure - it's a general question, but I wasn't sure how to word it...

Say I have something like this:
public class Class1
{
   public Class1()
  {
  }

  string _someProperty;
  [ACustomAttribute("Some Custom Attribute Value")]
  public string SomeProperty
  {
    get { return _someProperty; }
    set { _someProperty = value; }
  }
}

I simply want to use reflection to get "Some Custom Attribute Value" from
the ACustomAttribute attribute in a program that loads the assembly that
contains Class1.  I can use GetCustomAttributes to get the attribute itself,
but I can't quite get how to get the value of the parameter contained therein.

Does that make sense?  I can see what I want in my head, but getting it out
of there might be a problem.... : )

Thanks,
pagates

> pagates,
>
[quoted text clipped - 18 lines]
> > Thanks,
> > pagates
Nicholas Paldino [.NET/C# MVP] - 29 Mar 2007 17:01 GMT
pagates,

   Ok, I understand now.

   So, when you have the attribute like this:

[ACustomAttribute("Some Custom Attribute Value")]
public string SomeProperty

   The runtime constructs an instance of ACustomAttribute passing the value
of "Some Custom Attribute Value".  What the constructor does with this is up
to the attribute class.  It's like any other class in this sense.
Typically, however, it will be exposed as a property.  So, on the
ACustomAttribute class, there should be some sort of property that exposes
the value.  When you call GetCustomAttributes, you can cast the return value
to an instance of ACustomAttribute and then access the property to get the
value (assuming it is exposed as such).

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi Nicholas,
>
[quoted text clipped - 54 lines]
>> > Thanks,
>> > pagates
bonk - 14 May 2007 09:51 GMT
This approach is only possible if you can actually cast to the
CustomAttribute. If I reflect on an Assembly that also declares the
CustomAttribute Type I will not be able to cast to this Attribute type
because I do not reference that assembly. How could this issue be
solved ?

On Mar 29, 6:01 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
> pagates,
>
[quoted text clipped - 82 lines]
>
> - Show quoted text -
Marc Gravell - 14 May 2007 10:04 GMT
Fist - you can't access *parameters*, but you can access *properties*;
and there-in lies the key.

If you don't want to reference the assembly, then perhaps use the
System.ComponentModel to access the values at runtime - i.e. (notepad
code; not compile-tested)

foreach(Attribute untypedAttribute in ...) { // presumably
GetAttributes() etc
 Debug.WriteLine(untypedAttribute.GetType().Name);
 foreach(PropertyDescriptor property in
TypeDescriptor.GetProperties(untypedAttribute)) {
   Debug.WriteLine(string.Format("{0}={1}", property.Name,
property.GetValue(untypedAttribute)));
 }
}

If you want to work with a *specific* attribute, then my first choice
would be to reference it! Otherwise, perhaps filter on FQN on
FullName.

Marc

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.