none
Attributes (System.Attribute) --> How to change attribute values in the instance RRS feed

  • Question

  • Hello techies,

    Can the custom "Attributes" applied at the "Class" level be changed dynamically in the instance member?
    I have a piece of code that works fine but I can't achieve the expected result.

    TestAtrribute.cs

    using System.Text;
    
    namespace AttributesDemo
    {
        [AttributeUsage(AttributeTargets.Class)]
        public class TestAttribute : Attribute
        {
            public int TheNumber;
    
            public string Name;
    
            public TestAttribute(int number)
            {
                TheNumber = number;
            }
    
            public void PrintOut()
            {
                Console.WriteLine("\tTheNumber = {0}", TheNumber);
                Console.WriteLine("\tName = \"{0}\"", Name);
            }
        }
    }
    


    TestClasses.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace AttributesDemo
    {
        [Test(3)]
        public class TestClassA
        {
            public TestClassA()
            {
    
            }
    
        }
    
        [Test(4, Name = "TestClassB")]
        public class TestClassB
        {
            public TestClassB()
            {
    
            }
        }
    
    }
    

    Form1.cs

    private void Form1_Load(object sender, EventArgs e)
            {
                TestClassA a = new TestClassA();
                Type type = a.GetType();
                TestAttribute[] AttributeArray = (TestAttribute[])type.GetCustomAttributes(typeof(TestAttribute), false);
                if (AttributeArray.Length != 0)
                {
                    TestAttribute ta = AttributeArray[0];
                    ta.TheNumber = 100;
                    ta.Name = "Clement ALBERT";                
                    ta.PrintOut();                
                }
                PrintTestAttributes(a);
           }
                  
            public static void PrintTestAttributes(object obj)
            {
                Type type = obj.GetType();
                TestAttribute[] AttributeArray = (TestAttribute[])type.GetCustomAttributes(typeof(TestAttribute),false);
                Console.WriteLine("Class:\t{0}", type.Name);
                if (AttributeArray.Length == 0)
                {
                    Console.WriteLine("There are no TestAttributes applied to this class {0}",type.Name);
                    return;
                }
                TestAttribute ta = AttributeArray[0];
                ta.PrintOut();
            }
    


    In the Form_Load event I'm able to change the existing values of the attributes on the instance members.
    But when I recheck the values by calling PrintTestAttributes it has the values set intially using the attribute declaration.
    What I ideally want is have attributes that are configurable (read/write) from my object instances.

    Awaiting your valuable inputs.

    Clement


    Tuesday, January 10, 2012 1:23 PM

Answers

  • Hey Clement,

    I think you're using the wrong feature. Attributes are used to query metadata at runtime and not to change it. You can set some values on attributes in the code and that'll be written to your assembly metadata.

    You can check the values at run time but cannot set it.

    About the behaviour that you noticed, it seems that it return a new TestAttribute object every time so the values set by you are lost.


    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    • Marked as answer by Paul Zhou Tuesday, January 31, 2012 8:29 AM
    Wednesday, January 11, 2012 9:42 AM