Adding attribute to System types

  • Thread starter Thread starter astruyk
  • Start date Start date
A

astruyk

Is there a way to add an Attribute to an existing type? Like say I
wanted to add a [MyCustomAttribute] to the List<int> type, how would I
go about doing that?

Also is there any way to attach Attributes to types at runtime?
 
You can add attributes to type at runtime but only in a very special
circumstance.

Any object subject to reflection can be given a TypeDescriptionProvider.
This enables the TypeDescriptor to return any custom information yo may
desire, including dynamically created attributes, extra properties and
so-on.

Normally attributes are compiler generated, fixed and only instantiated when
reflection is used.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Hi,

Why do you need to add an attribute to an FCL type?

(BTW, you couldn't add an attribute to List<int> anyway since it doesn't
exist. List<T> is the class).

You could derive a class from List<int> and add a custom attribute to that
if you want.
 
I'm using Attributes to indicate to a class what types should be
ignored while processing data using reflection. Using the
TypeDescriptor seems to be working, as long as you check for the
Attribute using the TypeDescriptor and not just the
Type.GetCustomAttributes()...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top