예제 #1
0
        public static void ReflectionTest10()
        {
            Tx obj = new Tx {
                FloatField = 21, IntField = 21
            };
            Type t      = obj.GetType();
            var  fields = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var  info   = fields[0]; //FloatField

            object value = info.GetGetMethod().Invoke(obj, null);

            Console.WriteLine(string.Format("{0} = {1}", info.Name, value));

            if (value == null)
            {
                throw new Exception("null obj - FloatField"); // 不应该走到这里来,但走到了这里
            }

            info = fields[1];

            value = info.GetGetMethod().Invoke(obj, null);
            Console.WriteLine(string.Format("{0} = {1}", info.Name, value));

            if (value == null)
            {
                throw new Exception("null obj - IntField");
            }
            else
            {
                Console.WriteLine("not null obj - IntField"); // 对于int是正确的,走到了这里
            }
        }