Пример #1
0
        /// <summary>
        /// Creates a <see cref="ReflectedClass"/> from a <paramref name="type"/>.
        /// </summary>
        /// <param name="type">Type from which the reflected class will be created.</param>
        public ReflectedClass Create(Type type)
        {
            var reflectedClass = new ReflectedClass();

            reflectedClass.type = type;

            var constructor = this.ResolveConstructor(type);

            if (constructor != null)
            {
                if (constructor.GetParameters().Length == 0)
                {
                    reflectedClass.constructor = MethodUtils.CreateConstructor(type, constructor);
                }
                else
                {
                    reflectedClass.paramsConstructor = MethodUtils.CreateConstructorWithParams(type, constructor);
                    ;
                }
            }

            reflectedClass.constructorParameters = this.ResolveConstructorParameters(constructor);
            reflectedClass.methods    = this.ResolveMethods(type);
            reflectedClass.properties = this.ResolveProperties(type);
            reflectedClass.fields     = this.ResolveFields(type);

            return(reflectedClass);
        }
Пример #2
0
        /// <summary>
        /// 返回一个储存了指定类型的反射信息的 ReflectInfo 类
        /// </summary>
        virtual public ReflectionInfo Create(Type type)
        {
            // 新建一个 ReflectionInfo 类
            var reflectionInfo = new ReflectionInfo();

            // 获取 ReflectInfo 类的信息
            reflectionInfo.type = type;
            var constructor = this.GetPreferredConstructor(type);

            // 由于带参数和不带参数的构造函数委托类型不同,将筛选构造函数的过程封装为一个独立的方法将带来不必要的装、拆箱步骤,所以直接在方法内处理
            if (constructor != null)
            {
                if (constructor.GetParameters().Length == 0)
                {
                    reflectionInfo.constructor = MethodUtils.CreateConstructor(type, constructor);
                }
                else
                {
                    reflectionInfo.paramsConstructor = MethodUtils.CreateConstructorWithParams(type, constructor);
                }
            }

            reflectionInfo.constructorParameters = GetConstructorParameters(constructor);
            reflectionInfo.methods    = GetMethods(type);
            reflectionInfo.properties = GetProperties(type);
            reflectionInfo.fields     = GetFields(type);

            return(reflectionInfo);
        }