关于特性的理解和使用

将特性插入,就是插入一个标志?然后再类typeof中存在一(字段?方法?)对多(特性)

 

当调用特性的方法的时候就是调用想对应类的 方法;

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 特性
{
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A() {mypeproty=""};
            bAttribute.IsPropetyNull(a);
            aAttribute.print();
            System.Console.Read();
        }
        public class bAttribute : System.Attribute
        {
            /// <summary>
            /// 检查应用是否为空?为空返回true;
            /// </summary>
            /// <param name="ob"></param>
            /// <returns></returns>
            static public bool IsPropetyNull(object ob)
            {
                var ObType = ob.GetType();
                foreach (var propety in ObType.GetProperties())
                {
                    var atrributes = propety.GetCustomAttributes(typeof(bAttribute), false);
                    if (atrributes.Length > 0)
                    {
                        if (propety.GetValue(ob) == null)

                        {
                            Console.WriteLine("为空");
                            return true;
                        }
                        else
                        {
                            Console.WriteLine("不为空");
                            return false;
                        }
                    }



                }
                Console.WriteLine("不为空");
                return false;
            }

        }
        public class aAttribute : System.Attribute
        {
            public static void print()
            {
                Console.WriteLine("hello, a Attribute!");
            }
        }

        class A
        {
           [bAttribute]
            

            public string mypeproty
            {
                set;
                get;
            }
            //[a]
            [bAttribute]
            public string myid
            {
                set;
 1 using System.Reflection;
 2 
 3 // An enumeration of animals. Start at 1 (0 = uninitialized).
 4 public enum Animal
 5 {
 6     // Pets.
 7     Dog = 1,
 8     Cat,
 9     Bird,
10 }
11 
12 // A custom attribute to allow a target to have a pet.
13 public class AnimalTypeAttribute : Attribute
14 {
15     // The constructor is called when the attribute is set.
16     public AnimalTypeAttribute(Animal pet)
17     {
18         thePet = pet;
19         Console.WriteLine("what a fuck programe!");
20     }
21 
22     // Keep a variable internally ...
23     protected Animal thePet;
24 
25     // .. and show a copy to the outside world.
26     public Animal Pet
27     {
28         get { return thePet; }
29         set { thePet = value; }
30     }
31 }
32 
33 // A test class where each method has its own pet.
34 class AnimalTypeTestClass
35 {
36     [AnimalType(Animal.Dog)]
37     public void DogMethod() { }
38 
39     [AnimalType(Animal.Cat)]
40     public void CatMethod() { }
41 
42     [AnimalType(Animal.Bird)]
43     public void BirdMethod() { }
44 }
45 
46 class DemoClass
47 {
48     static void Main(string[] args)
49     {
50         AnimalTypeTestClass testClass = new AnimalTypeTestClass();
51         Type type = testClass.GetType();
52         // Iterate through all the methods of the class.
53         foreach (MethodInfo mInfo in type.GetMethods())
54         {
55             // Iterate through all the Attributes for each method.
56             foreach (Attribute attr in
57                 Attribute.GetCustomAttributes(mInfo))
58             {
59                 // Check for the AnimalType attribute.
60                 if (attr.GetType() == typeof(AnimalTypeAttribute))
61                     Console.WriteLine(
62                         "Method {0} has a pet {1} attribute.",
63                         mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
64             }
65 
66            
67         }
68         Console.Read();
69     }
70 }
71 /*
72  * Output:
73  * Method DogMethod has a pet Dog attribute.
74  * Method CatMethod has a pet Cat attribute.
75  * Method BirdMethod has a pet Bird attribute.
76  */

 

get;

            }

        }
    }
}

 

以上

IsPropetyNull 是自定义的特性,它并没有改写 system.Attribute 的特性函数;

对于特性可以标志(?)字段或者方法的时候 调用构造函数,然后当需要的时候 创建对象?
以上是msdn上的 例子




系统定义的特性有哪些?

 

using System.Reflection;
// An enumeration of animals. Start at 1 (0 = uninitialized).public enum Animal{    // Pets.    Dog = 1,    Cat,    Bird,}
// A custom attribute to allow a target to have a pet.public class AnimalTypeAttribute : Attribute{    // The constructor is called when the attribute is set.    public AnimalTypeAttribute(Animal pet)    {        thePet = pet;        Console.WriteLine("what a fuck programe!");    }
    // Keep a variable internally ...    protected Animal thePet;
    // .. and show a copy to the outside world.    public Animal Pet    {        get { return thePet; }        set { thePet = value; }    }}
// A test class where each method has its own pet.class AnimalTypeTestClass{    [AnimalType(Animal.Dog)]    public void DogMethod() { }
    [AnimalType(Animal.Cat)]    public void CatMethod() { }
    [AnimalType(Animal.Bird)]    public void BirdMethod() { }}
class DemoClass{    static void Main(string[] args)    {        AnimalTypeTestClass testClass = new AnimalTypeTestClass();        Type type = testClass.GetType();        // Iterate through all the methods of the class.        foreach (MethodInfo mInfo in type.GetMethods())        {            // Iterate through all the Attributes for each method.            foreach (Attribute attr in                Attribute.GetCustomAttributes(mInfo))            {                // Check for the AnimalType attribute.                if (attr.GetType() == typeof(AnimalTypeAttribute))                    Console.WriteLine(                        "Method {0} has a pet {1} attribute.",                        mInfo.Name, ((AnimalTypeAttribute)attr).Pet);            }
                   }        Console.Read();    }}/* * Output: * Method DogMethod has a pet Dog attribute. * Method CatMethod has a pet Cat attribute. * Method BirdMethod has a pet Bird attribute. */

 

posted @ 2020-09-14 18:28  兰梦书生  阅读(164)  评论(0)    收藏  举报