예제 #1
0
        public void Example_01()
        {
            ReflectClass rc  = new ReflectClass();
            Type         t   = rc.GetType();
            object       obj = Activator.CreateInstance(t);

            FieldInfo address = t.GetField("Address");

            address.SetValue(obj, "Beijing");

            PropertyInfo name = t.GetProperty("Name");

            name.SetValue(obj, "wang", null);

            PropertyInfo age = t.GetProperty("Age");

            age.SetValue(obj, 20, null);

            MethodInfo method = t.GetMethod("Show");

            method.Invoke(obj, null);

            Console.WriteLine("Address为:" + ((ReflectClass)obj).Address);
            Assert.IsTrue(true);
        }
예제 #2
0
        public void GetPublicMethod()
        {
            Type t = new ReflectClass().GetType();

            MethodInfo[] mi = t.GetMethods();
            foreach (MethodInfo method in mi)
            {
                Console.WriteLine(method.ReturnType + "|" + method.Name);
            }
            Assert.IsTrue(true);
        }
예제 #3
0
        public void GetProperties()
        {
            Type t = new ReflectClass().GetType();

            PropertyInfo[] propertyInfos = t.GetProperties();
            foreach (PropertyInfo p in propertyInfos)
            {
                Console.WriteLine(p.Name);
            }
            Assert.IsTrue(true);
        }
예제 #4
0
        public void GetField()
        {
            Type t = new ReflectClass().GetType();

            FieldInfo[] fieldInfos = t.GetFields(BindingFlags.Public);
            foreach (FieldInfo fieldInfo in fieldInfos)
            {
                Console.WriteLine(fieldInfo.Name);
            }
            Assert.IsTrue(true);
        }
예제 #5
0
        public void GetConstructors()
        {
            Type t = new ReflectClass().GetType();

            // 获取类的所有构造函数
            ConstructorInfo[] constructorInfos = t.GetConstructors();
            foreach (ConstructorInfo ci in constructorInfos)
            {
                // 获取每个构造函数的参数
                ParameterInfo[] parameterInfos = ci.GetParameters();
                foreach (ParameterInfo p in parameterInfos)
                {
                    Console.WriteLine(p.ParameterType.ToString() + "\n" + p.Name + "\n");
                }
            }
            Assert.IsTrue(constructorInfos.Length > 0);
        }