473,414 Members | 1,777 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Adding attributes at runtime


hi

Is there a "simple" way to add attributes to a class/property at
runtime?

What I try to do is set the default editor for a class/property at
runtime (I know I can set this very easy by decorating the class/
property code), but I would like to do this at runtime.

Is there another way to tell the PropertyGrid what editor to use for a
class/property ?

thanks,
florin

Aug 15 '07 #1
5 3655
florin,

If you want to add attributes to a type at runtime, then you really
can't, as a type is supposed to be static throughout the lifetime of an app,
and that includes attributes.

However, the PropertyGrid uses Type Descriptors, which have a more
limited context, and will give you what you want. Basically, you want to
create a shim class which implements the ICustomTypeDescriptor interface,
and return the property descriptors, method descriptors, and all the as well
as the Attributes that are associated with it.

Check out the .NET matters column in MSDN Magazine from April and May
2005 for an article which will help you with what you are looking for:

http://msdn.microsoft.com/msdnmag/is...04/NETMatters/
http://msdn.microsoft.com/msdnmag/is...05/NETMatters/
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"florin" <df****@gmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
>
hi

Is there a "simple" way to add attributes to a class/property at
runtime?

What I try to do is set the default editor for a class/property at
runtime (I know I can set this very easy by decorating the class/
property code), but I would like to do this at runtime.

Is there another way to tell the PropertyGrid what editor to use for a
class/property ?

thanks,
florin

Aug 15 '07 #2
Note that when talking about a Type (rather than an instance), then
the 2.0 TypeDescriptionProvider implementation can be more versatile.

Something else to look at: TypeDescriptor.AddAttributes()
http://msdn2.microsoft.com/en-us/lib...tw(VS.80).aspx

I've never used it, but it looks promising. Note that this only
affects the *component-model* (which is what any UI work should be
using anyway) - it would affect reflection, which looks at the
definitions at the point of compilation.

Marc

Aug 15 '07 #3
Replace:

it would affect reflection

With:

it wouldn't affect reflection
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
Note that when talking about a Type (rather than an instance), then
the 2.0 TypeDescriptionProvider implementation can be more versatile.

Something else to look at: TypeDescriptor.AddAttributes()
http://msdn2.microsoft.com/en-us/lib...tw(VS.80).aspx

I've never used it, but it looks promising. Note that this only
affects the *component-model* (which is what any UI work should be
using anyway) - it would affect reflection, which looks at the
definitions at the point of compilation.

Marc

Aug 15 '07 #4
You are absolutely right... slippy fingers - honest! I hope it was
clear from context that this is what I meant ;-(

Marc
Aug 16 '07 #5
ok, thanks guys!

at least right now I have a starting point, an not 5 :)

florin

Aug 16 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Maersa | last post by:
hi, i'm trying to add "attributes" to some nodes and it works at design time but fails at runtime. XmlDocument doc = new XmlDocument(); XmlElement elem = doc.CreateElement( "root" );...
9
by: James Geurts | last post by:
Hey all... I posted this in the vs.net ide group too, but people are not answering, so I figured that it might be more appropriate here. I'm not sure if I'm adding a designer to my code properly. ...
1
by: Rob | last post by:
I would like to add the Serializable attribute to an object at runtime. I want to send a System.Web.Mail.MailMessage over the line using Remoting, but MailMessage is not serializable. Thanks in...
3
by: Robert Bull | last post by:
I am trying to add a radio button at runtime in an asp.net web form but I keep getting the error message "type 'RadioButton' must be placed inside a form tag with runat=server". I use the following...
5
by: Steven Berkovitz | last post by:
Hi there, I am having a problem where if i add an attribute to a control at runtime the rendered attribute is HTML encoded. For example, on a textbox: textBox.Attributes = "if(x && y)...
2
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article...
3
by: astruyk | last post by:
Is there a way to add an Attribute to an existing type? Like say I wanted to add a to the List<inttype, how would I go about doing that? Also is there any way to attach Attributes to types at...
3
by: zslevi | last post by:
Can I access the class attributes from a method added at runtime? (My experience says no.) I experimented with the following code: class myclass(object): myattr = "myattr" instance =...
1
by: ton | last post by:
Hi, I want to add several textbox to my form during runtime: the code is very simple Lab = New TextBox Lab.ID = "A" & i Lab.Height = 200 Lab.Visible = True Lab.Text = "test"
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.