Exemplo n.º 1
0
        public static void Print_the_Result_of_Attribute(CozyTestClass n)
        {
            Type         typeInfo      = typeof(CozyTestClass);
            PropertyInfo CozyProInfo   = typeInfo.GetProperty("TestValue");
            var          CozyProAttrib = CozyProInfo.GetCustomAttribute <CozyMinMaxAttribute>() as CozyMinMaxAttribute;
            int          result        = 0;

            if (CozyProAttrib != null)
            {
                int minValue = CozyProAttrib.minValue;
                int maxValue = CozyProAttrib.maxValue;
                if (n.TestValue < minValue)
                {
                    result = minValue;
                }
                else if (n.TestValue > maxValue)
                {
                    result = maxValue;
                }
            }
            else
            {
                result = n.TestValue;
            }
            Console.WriteLine("{0} is {1}", CozyProInfo.Name, result);
        }
Exemplo n.º 2
0
        // 判断CozyMinMax特性之后输出CozyTestClass类中TestValue的值
        public static void Use_by_Attribute_CozyMinMax()
        {
            CozyTestClass ctc = new CozyTestClass();

            ctc.TestValue = 101;
            Print_the_Result_of_Attribute(ctc);
        }
Exemplo n.º 3
0
 public static void Print_the_Result_of_Attribute(CozyTestClass n)
 {
     Type typeInfo = typeof(CozyTestClass);
     PropertyInfo CozyProInfo = typeInfo.GetProperty("TestValue");
     var CozyProAttrib = CozyProInfo.GetCustomAttribute<CozyMinMaxAttribute>() as CozyMinMaxAttribute;
     int result = 0;
     if (CozyProAttrib != null)
     {
         int minValue = CozyProAttrib.minValue;
         int maxValue = CozyProAttrib.maxValue;
         if (n.TestValue < minValue)
         {
             result = minValue;
         }
         else if (n.TestValue > maxValue)
         {
             result = maxValue;
         }
     }
     else
     {
         result = n.TestValue;
     }
     Console.WriteLine("{0} is {1}", CozyProInfo.Name, result);
 }
Exemplo n.º 4
0
 // 判断CozyMinMax特性之后输出CozyTestClass类中TestValue的值
 public static void Use_by_Attribute_CozyMinMax()
 {
     CozyTestClass ctc = new CozyTestClass();
     ctc.TestValue = 101;
     Print_the_Result_of_Attribute(ctc);
 }