Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add attributes to an object property at runtime?

For example I want to remove or change below property attributes or add a new one. Is it possible?

[XmlElement("bill_info")]
[XmlIgnore]
public BillInfo BillInfo
{
  get { return billInfo; }
  set { billInfo = value; }
}
like image 408
NetSide Avatar asked Mar 02 '09 13:03

NetSide


People also ask

How to add properties to a class in Python?

You cannot add a new property() to an instance at runtime, because properties are data descriptors. Instead you must dynamically create a new class, or overload __getattribute__ in order to process data descriptors on instances.

How do attributes work in C#?

In C#, attributes are classes that inherit from the Attribute base class. Any class that inherits from Attribute can be used as a sort of "tag" on other pieces of code. For instance, there is an attribute called ObsoleteAttribute . This is used to signal that code is obsolete and shouldn't be used anymore.

What is custom attribute in C#?

Attributes are used to impose conditions or to increase the efficiency of a piece of code. There are built-in attributes present in C# but programmers may create their own attributes, such attributes are called Custom attributes. To create custom attributes we must construct classes that derive from the System.


1 Answers

(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.

like image 74
Marc Gravell Avatar answered Sep 28 '22 11:09

Marc Gravell