コード例 #1
0
        /// <summary>
        /// validation on method
        /// </summary>
        /// <param name="methodName"></param>
        private void Validation(string methodName)
        {
            var stuType = Student.GetType();

            if (stuType.IsDefined(typeof(CustomerAttribute), true))
            {
                MethodInfo methodInfo = stuType.GetMethod(methodName);

                CustomerAttribute ca   = methodInfo.GetCustomAttribute(typeof(CustomerAttribute)) as CustomerAttribute;
                bool isValidNameLength = ca.Validate(Student.Name);
                if (isValidNameLength)
                {
                    Console.WriteLine("valid student name");
                    if (methodInfo.Name.Equals(sayHiMethodName))
                    {
                        methodInfo.Invoke(Student, null);
                    }
                    else if (methodInfo.Name.Equals(answerMethodName))
                    {
                        methodInfo.Invoke(Student, new object[] { "Liangfengyouxing" });
                    }
                }
            }
        }