Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I need to change the property of an attribute at run time.

Example:
public partial class Class1
{
  [Attr1("")]
  public string ID { get; set; }
}

public class Attr1: Attribute
{
   public string Type { get; set;}

   public Attr1() { }

   public Attr1(string type)
   {
     Type = type
   }
}

When testing:
Class1 c = new Class1();
c.ID = "ID1";
PropertyInfo propPK = c.GetType().GetProperty("ID");
object[] attrs = propPK.GetCustomAttributes(typeof(Attr1), false);
if (attrs.Length > 0)
{
  object attr = attrs.First();
  attr.GetType().GetProperty("Type").SetValue(attr, "Type1" , null);
}


So when I check the [C] object in order the check if the property of the attribute is set, I see it blank...
Posted

1 solution

Sorry, you cannot do that. Custom attributes are readonly and set only at compile time. You'd be better off going another route.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900