Change Custom Attribute's Parameter at Runtime

Change custom attribute's parameter at runtime

You cannot change attributes at runtime. They are embedded into the metadata of the assembly. Your method is changing the internal state of a particular instance; but when you load the attribute again, you are getting a different instance.

Change values in Class Attribute at runtime

There is no implicit connection between attributes and objects instances. Only between the class and the attribute. The best bet would be to look for the attribute in the constructor and "cache" the values in properties on the object. Of course that doesn't make sense if you are only looking at the Test class, but it does make sense if the constructor of the SuperClass looks for the custom attributes on the type retrieved with "this.GetType()".

Is it possible to modify the attribute of a property at runtime?

It depends; from a reflection perspective: no. You can't. But if you are talking about attributes used by System.ComponentModel in things like data-binding, they you can use TypeDescriptor.AddAttributes to append extra attributes. Or other customer models involving custom descriptors. So it depends on the use-case.


In the case of xml serialization, it gets more interesting. Firstly, we can use fun object models:

using System;
using System.Xml.Serialization;
public class MyData
{
[XmlAttribute]
public int Id { get; set; }
[XmlAttribute]
public string Name { get; set; }
[XmlIgnore]
public bool NameSpecified { get; set; }

static void Main()
{
var ser = new XmlSerializer(typeof(MyData));

var obj1 = new MyData { Id = 1, Name = "Fred", NameSpecified = true };
ser.Serialize(Console.Out, obj1);
Console.WriteLine();
Console.WriteLine();
var obj2 = new MyData { Id = 2, Name = "Fred", NameSpecified = false };
ser.Serialize(Console.Out, obj2);
}
}

The bool {name}Specified {get;set;} pattern (along with bool ShouldSerialize{name}()) is recognised and used to control which elements to include.

Another alternative is to use the non-default ctor:

using System;
using System.Xml.Serialization;
public class MyData
{
[XmlAttribute]
public int Id { get; set; }
public string Name { get; set; }

static void Main()
{
var obj = new MyData { Id = 1, Name = "Fred" };

XmlAttributeOverrides config1 = new XmlAttributeOverrides();
config1.Add(typeof(MyData),"Name",
new XmlAttributes { XmlIgnore = true});
var ser1 = new XmlSerializer(typeof(MyData),config1);
ser1.Serialize(Console.Out, obj);
Console.WriteLine();
Console.WriteLine();
XmlAttributeOverrides config2 = new XmlAttributeOverrides();
config2.Add(typeof(MyData), "Name",
new XmlAttributes { XmlIgnore = false });
var ser2 = new XmlSerializer(typeof(MyData), config2);
ser2.Serialize(Console.Out, obj);
}
}

Note though that if you use this second approach you need to cache the serializer instance, as it emits an assembly every time you do this. I find the first approach simpler...

how to change Authorize attribute's parameter at runtime

Vivek,

The authorize attribute can be inherited from and you can override the OnAuthorize method. Something like below.

public sealed class AuthorizationAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
//do custom authorization here
base.OnAuthorization(filterContext);
}
}

At this point you can do anything you wish for authorization. You can add custom constructors to set your own variables. For example I currently use this override to detect if the user is authorized and they have access to a specific location.

Cheers



Related Topics



Leave a reply



Submit