Can I Add/Modify Attributes to class Type during run-time?

S

S.Sigal

Hello:

I've seen code in some books for databinding that handles attaching Attributes
to
Properties during runtime...which is very close (but no cigar) to what I am
looking for...

The code in the book was basically the following:

override protected void PreFilterProperties(IDictionary properties) {
base.PreFilterProperties(properties);
PropertyDescriptor prop = (PropertyDescriptor)properties["DataSource"];
if(prop!=null) {
System.ComponentModel.AttributeCollection runtimeAttributes =
prop.Attributes;
// make a copy of the original attributes but make room for one extra
attribute
//ie the TypeConverter attribute
Attribute[] attrs = new Attribute[runtimeAttributes.Count + 1];
runtimeAttributes.CopyTo(attrs, 0);
attrs[runtimeAttributes.Count] = new
TypeConverterAttribute(typeof(DataSourceConverter));
prop = TypeDescriptor.CreateProperty(this.GetType(), "DataSource",
typeof(string),attrs);
properties["DataSource"] = prop;
}...

Although this is not what I want -- it intrigues me, because one of the things I
wish to do is change the Designer attribute on a Control on the fly --
specifically, to change a Control's ControlDesigner attribute from standard
ControlDesigner to ReadWriteDesigner --- and back again -- to get around the
limitations that are in the IDE...

Only problem is that I don't see anywhere in the Framwork where to SET
attributes... I can read them, and can append to them as above snippet shows:
System.ComponentModel.AttributeCollection attrsExist = o.GetType().Attributes;
It's just the last line that I am missing....Something that would look like:
o.SetAttributes(attrsNew);

Any hope for this to succeed? I mean ANY way? Even if a nightmare....?

Thank you ,
Sky
 
J

Jared Parsons [MSFT]

You cannot modify the attributes of a compiled type in an assembly.

--
Jared Parson [MSFT]
(e-mail address removed)

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
S

S.Sigal

Thanks Jared.
It's what I suspected, but was hoping someone would prove me wrong...
Sky


Jared Parsons said:
You cannot modify the attributes of a compiled type in an assembly.

--
Jared Parson [MSFT]
(e-mail address removed)

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

S.Sigal said:
Hello:

I've seen code in some books for databinding that handles attaching
Attributes to
Properties during runtime...which is very close (but no cigar) to what I am
looking for...

The code in the book was basically the following:

override protected void PreFilterProperties(IDictionary properties) {
base.PreFilterProperties(properties);
PropertyDescriptor prop = (PropertyDescriptor)properties["DataSource"];
if(prop!=null) {
System.ComponentModel.AttributeCollection runtimeAttributes =
prop.Attributes;
// make a copy of the original attributes but make room for one extra
attribute
//ie the TypeConverter attribute
Attribute[] attrs = new Attribute[runtimeAttributes.Count + 1];
runtimeAttributes.CopyTo(attrs, 0);
attrs[runtimeAttributes.Count] = new
TypeConverterAttribute(typeof(DataSourceConverter));
prop = TypeDescriptor.CreateProperty(this.GetType(), "DataSource",
typeof(string),attrs);
properties["DataSource"] = prop;
}...

Although this is not what I want -- it intrigues me, because one of the
things I wish to do is change the Designer attribute on a Control on the
fly -- specifically, to change a Control's ControlDesigner attribute from
standard ControlDesigner to ReadWriteDesigner --- and back again -- to get
around the limitations that are in the IDE...

Only problem is that I don't see anywhere in the Framwork where to SET
attributes... I can read them, and can append to them as above snippet shows:
System.ComponentModel.AttributeCollection attrsExist =
o.GetType().Attributes;
It's just the last line that I am missing....Something that would look like:
o.SetAttributes(attrsNew);

Any hope for this to succeed? I mean ANY way? Even if a nightmare....?

Thank you ,
Sky
 

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

Top