1. 程式人生 > 實用技巧 >關於特性的理解和使用

關於特性的理解和使用

將特性插入,就是插入一個標誌?然後再類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; get; } } } }

系統定義的特性有哪些?