Example #1
0
        static void PrintMethod(Type t)
        {
            int    count   = 0;
            object fooinst = Activator.CreateInstance(t);

            MethodInfo[] listofmethod = t.GetMethods();
            Console.WriteLine("Custom Attr:");
            foreach (var method in listofmethod)
            {
                object[] attributearray = method.GetCustomAttributes(true);
                foreach (Attribute item in attributearray)
                {
                    if (item is MyCustomAttribute)
                    {
                        MyCustomAttribute customAttribute = (MyCustomAttribute)item;
                        Console.Write(method.Name + ": ");
                        bool value = (bool)method.Invoke(fooinst, null);
                        Console.WriteLine(value);



                        count++;
                    }
                }
            }
        }
Example #2
0
        public void PrintMyCustomAttribute()
        {
            MyCustomAttribute myAttribute = AttributeHelpers.GetClassCustomAttribute <MyCustomAttribute>(typeof(SomeClass));

            if (myAttribute == null)
            {
                Console.WriteLine("The Attribute was not found");
            }
            else
            {
                Console.WriteLine($"The Name is: {myAttribute.Name}");
                Console.WriteLine($"The Params are: {string.Join(", ", myAttribute.Params)}");
            }
        }