Saturday, July 6, 2013

CSharp C# – Method or Function Attributes Class and Used by and edited in a method inside the method or function

CSharp C#  – Method or Function Attributes Class and Used by and edited in a method inside the method or function.docx
Example code to show:
  1. How to create an attribute class
  2. How to refer to the current value or values of that attribute class being used on a method or function.
  3. Edit the value of an attribute field.

using System;
/*
        Show before a modification in the loop below:
        oNow.ToString()=TestingStuff.Program+MyPersonalAttribute
        ---------------------------------------------------------------
          mpa.PostionThisAttribute=2
          mpa.TextValue=My text value 2
        ---------------------------------------------------------------
        oNow.ToString()=TestingStuff.Program+MyPersonalAttribute
        ---------------------------------------------------------------
          mpa.PostionThisAttribute=0
          mpa.TextValue=My text value 0
        ---------------------------------------------------------------
        oNow.ToString()=TestingStuff.Program+MyPersonalAttribute
        ---------------------------------------------------------------
          mpa.PostionThisAttribute=1
          mpa.TextValue=My text value 1
        ---------------------------------------------------------------
        Show after modification in loop above:
        oNow.ToString()=TestingStuff.Program+MyPersonalAttribute
        ---------------------------------------------------------------
          mpa.PostionThisAttribute=2
          mpa.TextValue=My text value 2 and this also
        ---------------------------------------------------------------
        oNow.ToString()=TestingStuff.Program+MyPersonalAttribute
        ---------------------------------------------------------------
          mpa.PostionThisAttribute=0
          mpa.TextValue=My text value 0 and this also
        ---------------------------------------------------------------
        oNow.ToString()=TestingStuff.Program+MyPersonalAttribute
        ---------------------------------------------------------------
          mpa.PostionThisAttribute=1
          mpa.TextValue=My text value 1 and this also
        ---------------------------------------------------------------
        Press enter to finish
*/
namespace TestingStuff
{
    class Program
    {
        static void Main(string[] args)
        {
            DoStuff();
            Console.WriteLine("Press enter to finish");
            Console.ReadLine();
            
        }
        [MyPersonalAttribute(0,"My text value 0")]
        [MyPersonalAttribute(1, "My text value 1")]
        [MyPersonalAttribute(2, "My text value 2")]
        private static void DoStuff()
        {            
            object[] atts = new System.Diagnostics.StackFrame(0).GetMethod().GetCustomAttributes(true);
            Console.WriteLine("Show before a modification in the loop below:");
            foreach (object oNow in atts)
            {
                Console.WriteLine("oNow.ToString()={0}", oNow.ToString());
                if (string.Compare("TestingStuff.Program+MyPersonalAttribute", oNow.ToString(), true) == 0)
                {
                    Console.WriteLine();
                    Console.WriteLine(new string('-', 120));
                    MyPersonalAttribute mpa = (MyPersonalAttribute)oNow;
                    Console.WriteLine("  mpa.PostionThisAttribute={0}", mpa.PostionThisAttribute);
                    Console.WriteLine("  mpa.TextValue={0}", mpa.TextValue);
                    //Modify the text value:
                    mpa.TextValue += " and this also";
                    Console.WriteLine(new string('-', 120));
                }
            }
            Console.WriteLine("Show after modification in loop above:");
            foreach (object oNow in atts)
            {
                Console.WriteLine("oNow.ToString()={0}", oNow.ToString());
                if (string.Compare("TestingStuff.Program+MyPersonalAttribute", oNow.ToString(), true) == 0)
                {
                    Console.WriteLine();
                    Console.WriteLine(new string('-', 120));
                    MyPersonalAttribute mpa = (MyPersonalAttribute)oNow;
                    Console.WriteLine("  mpa.PostionThisAttribute={0}", mpa.PostionThisAttribute);
                    Console.WriteLine("  mpa.TextValue={0}", mpa.TextValue);
                    //Modify the text value:
                    mpa.TextValue += " and this also";
                    Console.WriteLine(new string('-', 120));
                }
            }
        }
        [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
        public class MyPersonalAttribute : Attribute
        {
            public MyPersonalAttribute(int postionThisAttribute, string textValue)
            {
                PostionThisAttribute = postionThisAttribute;
                TextValue = textValue;
            }
                        
            public int PostionThisAttribute { getprotected set; }
            public string TextValue { getset; }
        }
    }
}

No comments:

Post a Comment