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); }
// 判断CozyMinMax特性之后输出CozyTestClass类中TestValue的值 public static void Use_by_Attribute_CozyMinMax() { CozyTestClass ctc = new CozyTestClass(); ctc.TestValue = 101; Print_the_Result_of_Attribute(ctc); }
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); }