How to Add an Attribute to a Property at Runtime

Add Attributes to instance of object at runtime

I don't think you'll be able to accomplish this exactly, since attributes are static metadata.

Can attributes be added dynamically in C#?

A possible workaround could be to create your own custom Attribute that implements all the possible validations (MaxLength, Range, etc.) and have some trigger attached to the object to enable/disable them. This would handle the validation, but not the specific instance mentioned in your question (Html.LabelFor + DisplayName). For that, you could create your own extension method that accesses the DisplayName attribute in the same manner that you add it to the instance.

Can attributes be added dynamically in C#?

Attributes are static metadata. Assemblies, modules, types, members, parameters, and return values aren't first-class objects in C# (e.g., the System.Type class is merely a reflected representation of a type). You can get an instance of an attribute for a type and change the properties if they're writable but that won't affect the attribute as it is applied to the type.

How to add Xml attribute to a property at runtime

Try following :

    public class H204
{
[XmlAttribute(AttributeName = "Code")]
public string Code { get; set; }

[XmlElement(ElementName = "DW")]
public DW dw{ get; set; }
}
public class DW
{
[XmlAttribute(AttributeName = "Error")]
public string text { get; set; }
}

Add attribute on property of a runtime created type using reflection

It's been solved:

I had created StuckAttribute as an internal class. I've settled it setting the accesor class as public.

So, my test runs now:

PropertyInfo[] properties = flattenedType.GetProperties();
properties.Should().NotBeEmpty().And.HaveCount(4);

properties.Should().OnlyContain(p => p.GetCustomAttribute<Reflection.Classes.FieldPropertyOwnerAttribute>() != null);

Can I add attributes to an object property at runtime?

(edit - I misread the original question)

You cannot add actual attributes (they are burned into the IL); however, with XmlSerializer you don't have to - you can supply additional attributes in the constructor to the XmlSerializer. You do, however, need to be a little careful to cache the XmlSerializer instance if you do this, as otherwise it will create an additional assembly per instance, which is a bit leaky. (it doesn't do this if you use the simple constructor that just takes a Type). Look at XmlAttributeOverrides.

For an example:

using System;
using System.Xml.Serialization;
public class Person
{
static void Main()
{
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attribs = new XmlAttributes();
attribs.XmlIgnore = false;
attribs.XmlElements.Add(new XmlElementAttribute("personName"));
overrides.Add(typeof(Person), "Name", attribs);

XmlSerializer ser = new XmlSerializer(typeof(Person), overrides);
Person person = new Person();
person.Name = "Marc";
ser.Serialize(Console.Out, person);
}
private string name;
[XmlElement("name")]
[XmlIgnore]
public string Name { get { return name; } set { name = value; } }
}

Note also; if the xml attributes were just illustrative, then there is a second way to add attributes for things related to data-binding, by using TypeDescriptor.CreateProperty and either ICustomTypeDescriptor or TypeDescriptionProvider. Much more complex than the xml case, I'm afraid - and doesn't work for all code - just code that uses the component-model.

How to add property-level Attribute to the TypeDescriptor at runtime?

Unlike others have suggested, it's quite possible, and also not that hard. For example, you want to add some new attributes to some properties, which you can select at runtime based on some criteria.

There're two helper classes we'll need to implement this.

First goes PropertyOverridingTypeDescriptor, it allows us to supply our own property descriptors for some properties, while keeping others intact:

public class PropertyOverridingTypeDescriptor : CustomTypeDescriptor
{
private readonly Dictionary<string, PropertyDescriptor> overridePds = new Dictionary<string, PropertyDescriptor>();

public PropertyOverridingTypeDescriptor(ICustomTypeDescriptor parent)
: base(parent)
{ }

public void OverrideProperty(PropertyDescriptor pd)
{
overridePds[pd.Name] = pd;
}

public override object GetPropertyOwner(PropertyDescriptor pd)
{
object o = base.GetPropertyOwner(pd);

if (o == null)
{
return this;
}

return o;
}

public PropertyDescriptorCollection GetPropertiesImpl(PropertyDescriptorCollection pdc)
{
List<PropertyDescriptor> pdl = new List<PropertyDescriptor>(pdc.Count+1);

foreach (PropertyDescriptor pd in pdc)
{
if (overridePds.ContainsKey(pd.Name))
{
pdl.Add(overridePds[pd.Name]);
}
else
{
pdl.Add(pd);
}
}

PropertyDescriptorCollection ret = new PropertyDescriptorCollection(pdl.ToArray());

return ret;
}

public override PropertyDescriptorCollection GetProperties()
{
return GetPropertiesImpl(base.GetProperties());
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return GetPropertiesImpl(base.GetProperties(attributes));
}
}

Few remarks:

  • Constructor takes ICustomTypeDescriptor, no worries here, we can get one for any type or it's instance with the TypeDescriptor.GetProvider(_settings).GetTypeDescriptor(_settings) where _settings can be either Type or object of that type.
  • OverrideProperty does just what we need, more on it later.

The other class we need is the TypeDescriptionProvider that will return our custom type descriptor instead of the default one. Here it is:

public class TypeDescriptorOverridingProvider : TypeDescriptionProvider
{
private readonly ICustomTypeDescriptor ctd;

public TypeDescriptorOverridingProvider(ICustomTypeDescriptor ctd)
{
this.ctd = ctd;
}

public override ICustomTypeDescriptor GetTypeDescriptor (Type objectType, object instance)
{
return ctd;
}
}

Fairly simple: you just supply the type descriptor instance on construction and here you go.

And finally, processing code. For example, we want all properties ending with ConnectionString in our object (or type) _settings to be editable with the System.Web.UI.Design.ConnectionStringEditor. To achieve that, we can use this code:

// prepare our property overriding type descriptor
PropertyOverridingTypeDescriptor ctd = new PropertyOverridingTypeDescriptor(TypeDescriptor.GetProvider(_settings).GetTypeDescriptor(_settings));

// iterate through properies in the supplied object/type
foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(_settings))
{
// for every property that complies to our criteria
if (pd.Name.EndsWith("ConnectionString"))
{
// we first construct the custom PropertyDescriptor with the TypeDescriptor's
// built-in capabilities
PropertyDescriptor pd2 =
TypeDescriptor.CreateProperty(
_settings.GetType(), // or just _settings, if it's already a type
pd, // base property descriptor to which we want to add attributes
// The PropertyDescriptor which we'll get will just wrap that
// base one returning attributes we need.
new EditorAttribute( // the attribute in question
typeof (System.Web.UI.Design.ConnectionStringEditor),
typeof (System.Drawing.Design.UITypeEditor)
)
// this method really can take as many attributes as you like,
// not just one
);

// and then we tell our new PropertyOverridingTypeDescriptor to override that property
ctd.OverrideProperty(pd2);
}
}

// then we add new descriptor provider that will return our descriptor instead of default
TypeDescriptor.AddProvider(new TypeDescriptorOverridingProvider(ctd), _settings);

That's it, now all properties ending with ConnectionString will be editable through ConnectionStringEditor.

As you can see, we just override some functionality of the default implementation every time, so the system should be fairly stable and behave as expected.



Related Topics



Leave a reply



Submit