PC Review


Reply
Thread Tools Rate Thread

Adding attribute to System types

 
 
astruyk@gmail.com
Guest
Posts: n/a
 
      21st Dec 2006
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?

 
Reply With Quote
 
 
 
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      21st Dec 2006
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.



<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?
>



 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      22nd Dec 2006
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.

--
Dave Sexton

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?
>



 
Reply With Quote
 
astruyk@gmail.com
Guest
Posts: n/a
 
      22nd Dec 2006
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()...

Dave Sexton wrote:
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Are all types derived from system.object referenced types Tony Johansson Microsoft C# .NET 3 19th May 2010 02:54 AM
How can I convert the SQL data types to csharp system types.. Champika Nirosh Microsoft C# .NET 1 11th Nov 2004 09:25 AM
how to force which types custom attribute can be used with? Paweł Microsoft C# .NET 3 11th Nov 2004 06:19 AM
The type namespace.classname was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. Bob Rock Microsoft C# .NET 1 7th May 2004 05:51 PM
Convert from System. types to SQLServer types Gustavo De la Espriella Microsoft ADO .NET 1 28th Nov 2003 06:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:18 AM.