/// <summary>
		/// 返回使用反射优化后动态创建对应类型实例的结果
		/// </summary>
		/// <param name="instanceType">类型</param>
		/// <returns>类型实例</returns>
		public static object FastNew(this Type instanceType)
		{
			if( instanceType == null )
				throw new ArgumentNullException("instanceType");

			CtorDelegate ctor = (CtorDelegate)s_methodDict[instanceType];
			if( ctor == null ) {
				ConstructorInfo ctorInfo = instanceType.GetConstructor(Type.EmptyTypes);
				ctor = DynamicMethodFactory.CreateConstructor(ctorInfo);
				s_methodDict[instanceType] = ctor;
			}

			return ctor();
		}
示例#2
0
 internal DynamicCtor(Type declaringType, ConstructorInfo constructorInfo, AccessModifiers access,
                      VisibilityModifiers visibility,
                      CtorDelegate ctorDelegate) : base(constructorInfo)
 {
     CtorDelegate    = ctorDelegate ?? throw new ArgumentNullException(nameof(ctorDelegate));
     ConstructorInfo = constructorInfo;
     Access          = access;
     Visibility      = visibility;
     if (constructorInfo == null)
     {
         Parameters     = new ParameterInfo[0];
         ParameterTypes = Type.EmptyTypes;
     }
     else
     {
         Parameters     = constructorInfo.GetParameters();
         ParameterTypes = Parameters.Select(p => p.ParameterType).ToArray();
     }
 }
示例#3
0
        private void SetContext(bool buildIndex = false)
        {
            IEnumerable <PropertyInfo> props = GetType()
                                               .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                                               .Where(x => x.PropertyType.IsClass &&
                                                      x.PropertyType.IsGenericType &&
                                                      x.PropertyType.GetGenericTypeDefinition() == typeof(Rediset <>));


            foreach (PropertyInfo prop in props)
            {
                CtorDelegate ctor     = CreateConstructor(prop.PropertyType);
                Rediset      instance = (Rediset)ctor(this);
                prop.SetValue(this, instance);
                if (buildIndex)
                {
                    instance.BuildIndex();
                }
                _sets.Add(instance.EntityType, instance);
            }
        }
示例#4
0
        /// <summary>
        /// 根据指定的Type,用优化的方式快速创建实例
        /// </summary>
        /// <param name="instanceType"></param>
        /// <returns></returns>
        public static object FastNew(this Type instanceType)
        {
            if (instanceType == null)
            {
                throw new ArgumentNullException("instanceType");
            }

            CtorDelegate ctor = (CtorDelegate)s_methodDict[instanceType];

            if (ctor == null)
            {
                ConstructorInfo ctorInfo = instanceType.GetConstructor(Type.EmptyTypes);

                if (ctorInfo == null)
                {
                    throw new NotSupportedException(string.Format("类型\"{0}\"没有无参的构造方法。", instanceType.ToString()));
                }

                ctor = DynamicMethodFactory.CreateConstructor(ctorInfo);
                s_methodDict[instanceType] = ctor;
            }

            return(ctor());
        }
示例#5
0
        /// <summary>
        /// Invoke the constructor and create a new instance
        /// </summary>
        /// <exception cref="ArgumentException"></exception>
        public object Invoke(params object[] parameters)
        {
            var parameterCount = ParameterCount;

            if (parameters.Length >= parameterCount)
            {
                return(CtorDelegate.Invoke(parameters));
            }

            var oldParameters = parameters;

            parameters = new object[parameterCount];
            for (var i = oldParameters.Length + 1; i < parameterCount; i++)
            {
                var parameterInfo = Parameters[i];
                if (parameterInfo.IsOptional == false)
                {
                    throw new ArgumentException("Not enough parameters", nameof(parameters));
                }
                parameters[i] = parameterInfo.DefaultValue;
            }
            oldParameters.CopyTo(parameters, 0);
            return(CtorDelegate.Invoke(parameters));
        }
示例#6
0
 public EntityType( string name, ConstructorInfo ctorInfo )
 {
     m_Name = name;
     m_CtorInfo = ctorInfo;
     m_CtorDelegate = CreateCtorDelegate( ctorInfo );
 }
示例#7
0
 public EntityType(string name, ConstructorInfo ctorInfo)
 {
     m_Name         = name;
     m_CtorInfo     = ctorInfo;
     m_CtorDelegate = CreateCtorDelegate(ctorInfo);
 }
示例#8
0
 public EntityType(string name, ConstructorInfo ctorInfo)
 {
     Name         = name;
     Constructor  = ctorInfo;
     CtorDelegate = CreateCtorDelegate(ctorInfo);
 }