コード例 #1
0
 private static T New <T>(Type objType, IChoCustomObjectFactory customObjectFactory)
 {
     if (customObjectFactory == null)
     {
         return((T)ChoType.CreateInstance(objType));
     }
     //return (T)ChoRealProxy.GetProxy(ChoType.CreateInstance(objType));
     else
     {
         return((T)customObjectFactory.CreateInstance(objType));
     }
 }
コード例 #2
0
        public static object CreateInstance(Type configObjType)
        {
            if (configObjType == null)
            {
                throw new ArgumentNullException("configObjType");
            }

            lock (_cacheLockObj)
            {
                if (_globalConfigurationsCache.ContainsKey(configObjType))
                {
                    return(_globalConfigurationsCache[configObjType]);
                }
                else
                {
                    object configObject = ChoType.CreateInstance(configObjType);
                    object owner        = configObject;

                    if (ChoType.IsInterceptableObject(configObjType))
                    {
                        configObject = ChoInterceptingProxy.Instance(configObject);
                    }

                    //Load config file and assign values to object memebers
                    ChoConfigurationElementMapAttribute configurationElementMap = ChoType.GetAttribute(configObjType, typeof(ChoConfigurationElementMapAttribute)) as ChoConfigurationElementMapAttribute;
                    if (configurationElementMap == null)
                    {
                        throw new NullReferenceException(String.Format(CultureInfo.InvariantCulture, Resources.ES1001, configObjType.Name));
                    }

                    if (ChoType.IsConfigurableObject(configObjType))
                    {
                        ((ChoConfigurableObject)owner).ReadOnly = configurationElementMap.ReadOnly;
                    }

                    _globalConfigurationsCache[configObjType] = configObject;

                    configurationElementMap.Construct(configObject);

                    return(configObject);
                }
            }
        }
コード例 #3
0
        public virtual object CreateInstance()
        {
            if (ConverterType == null)
            {
                return(null);
            }

            if (ChoGuard.IsArgumentNotNullOrEmpty(Parameters) && ChoType.HasConstructor(ConverterType, Parameters))
            {
                return(ChoType.CreateInstance(ConverterType, Parameters));
            }
            else if (ChoType.HasConstructor(ConverterType, new object[] { String.Empty }))
            {
                return(ChoType.CreateInstance(ConverterType, new object[] { Parameters != null && Parameters.Length > 0 ? Parameters[0] : String.Empty }));
            }
            else
            {
                return(Activator.CreateInstance(ConverterType));
            }
        }
コード例 #4
0
        internal static object GetConfig(Type configObjectType, string configSectionPath, Type defaultConfigSectionHandlerType,
                                         ChoBaseConfigurationElement configElement)
        {
            if (_configuration == null)
            {
                return(null);
            }

            ChoConfigurationSection configurationSection = ChoConfigurationManager.GetConfigSection(configSectionPath);

            XmlNode[] restOfXmlElements = null;

            Type configSectionHandlerType = defaultConfigSectionHandlerType;

            if (configurationSection != null)
            {
                restOfXmlElements = configurationSection.RestOfXmlElements;
                if (!configurationSection.Type.IsNullOrWhiteSpace())
                {
                    configSectionHandlerType = ChoType.GetType(configurationSection.Type);
                }
            }

            configElement.ConfigSectionHandlerType = configSectionHandlerType;
            if (!typeof(IChoConfigurationSectionHandler).IsAssignableFrom(configSectionHandlerType))
            {
                throw new ChoConfigurationException("Configuration section handler type should be of IChoConfigurationSectionHandler type.");
            }

            if (typeof(IChoNullConfigurationSectionableHandler).IsAssignableFrom(configSectionHandlerType))
            {
                return(ChoType.CreateInstance <IChoConfigurationSectionHandler>(configSectionHandlerType).Create(configObjectType, null, restOfXmlElements, configElement));
            }
            else
            {
                XmlNode node = ChoXmlDocument.GetXmlNode(configSectionPath, _configuration.RestOfXmlDocumentElements);
                //if (node != null)
                return(ChoType.CreateInstance <IChoConfigurationSectionHandler>(configSectionHandlerType).Create(configObjectType, node, restOfXmlElements, configElement));
            }
            //return new ChoDefaultApplicationConfigSection(configObjectType);
        }
コード例 #5
0
ファイル: ChoSingletonHelper.cs プロジェクト: lanicon/Cinchoo
        /// <summary>
        /// Method to create or get the instance of the class T.
        /// </summary>
        /// <param name="args">Constructor parameters, if any</param>
        /// <returns>Instance of the class.</returns>
        internal T Instance(bool strict, object[] args)
        {
            T _instance = default(T);

            VerifySingtonType(strict);

            if (IsCustomSingleType())
            {
                ConstructorInfo constructorInfo = GetSingletonContructor();
                if (constructorInfo != null)
                {
                    try
                    {
                        _instance = constructorInfo.Invoke(args) as T;
                    }
                    catch (Exception innerEx)
                    {
                        throw new ChoSingletonException(String.Format("Failed to invoke constructor [{0}] in `{1}` type due mismatch parametes.",
                                                                      constructorInfo, typeof(T).FullName), innerEx);
                    }
                }
                else
                {
                    _instance = CreateInstance(args);
                }
            }
            else
            {
                _instance = ChoType.CreateInstance(typeof(T), args) as T;
            }

            if (_instance == null)
            {
                throw new ChoSingletonException("Instance object is null.");
            }

            return(_instance);
        }
コード例 #6
0
        public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
        {
            ICustomTypeDescriptor defaultDescriptor = base.GetTypeDescriptor(objectType, instance);

            ChoTypeProviderAttribute typeProviderAttribute = ChoType.GetAttribute <ChoTypeProviderAttribute>(GetType());

            if (typeProviderAttribute != null)
            {
                if (ChoGuard.IsArgumentNotNullOrEmpty(typeProviderAttribute.AdditionalConstructorParameters))
                {
                    return(ChoType.CreateInstance(typeProviderAttribute.Type,
                                                  ChoArray.Combine <object>(new object[] { this, defaultDescriptor, objectType, instance }, typeProviderAttribute.AdditionalConstructorParameters))
                           as ICustomTypeDescriptor);
                }
                else
                {
                    return(ChoType.CreateInstance(typeProviderAttribute.Type,
                                                  new object[] { this, defaultDescriptor, objectType, instance }) as ICustomTypeDescriptor);
                }

                //return instance == null ? defaultDescriptor : new FieldsToPropertiesTypeDescriptor(this, defaultDescriptor, objectType);
            }
            return(defaultDescriptor);
        }
コード例 #7
0
        public static object[] GetTypeConverters(MemberInfo memberInfo)
        {
            if (memberInfo == null)
            {
                return(null);
            }

            Type memberType;

            if (ChoType.TryGetMemberType(memberInfo, out memberType) && (memberType == null /*|| memberType.IsSimple() */))
            {
                return(null);
            }

            if (_typeMemberTypeConverterCache.ContainsKey(memberInfo))
            {
                if (_typeMemberTypeConverterCache[memberInfo] == EmptyTypeConverters)
                {
                    if (_typeTypeConverterCache.ContainsKey(memberType))
                    {
                        return(_typeTypeConverterCache[memberType]);
                    }
                }

                return(_typeMemberTypeConverterCache[memberInfo]);
            }
            else
            {
                lock (_typeMemberTypeConverterCacheLockObject)
                {
                    if (!_typeMemberTypeConverterCache.ContainsKey(memberInfo))
                    {
                        Type typeConverterAttribute = typeof(ChoTypeConverterAttribute);

                        _typeMemberTypeConverterCache[memberInfo]       = EmptyTypeConverters;
                        _typeMemberTypeConverterParamsCache[memberInfo] = EmptyParams;

                        ChoPriorityQueue queue       = new ChoPriorityQueue();
                        ChoPriorityQueue paramsQueue = new ChoPriorityQueue();
                        foreach (Attribute attribute in ChoType.GetAttributes(memberInfo, typeof(ChoTypeConverterAttribute), false))
                        {
                            ChoTypeConverterAttribute converterAttribute = (ChoTypeConverterAttribute)attribute;
                            if (converterAttribute != null)
                            {
                                queue.Enqueue(converterAttribute.Priority, converterAttribute.CreateInstance());
                                paramsQueue.Enqueue(converterAttribute.Priority, converterAttribute.Parameters);
                            }

                            if (queue.Count > 0)
                            {
                                _typeMemberTypeConverterCache[memberInfo]       = queue.ToArray();
                                _typeMemberTypeConverterParamsCache[memberInfo] = paramsQueue.ToArray();

                                return(_typeMemberTypeConverterCache[memberInfo]);
                            }
                        }

                        if (queue.Count == 0 && !memberType.IsSimple())
                        {
                            if (!_typeTypeConverterCache.ContainsKey(memberType))
                            {
                                foreach (Type type in ChoType.GetTypes <ChoTypeConverterAttribute>())
                                {
                                    ChoTypeConverterAttribute converterAttribute = type.GetCustomAttribute <ChoTypeConverterAttribute>();
                                    if (converterAttribute != null && converterAttribute.ConverterType == memberType)
                                    {
                                        _typeTypeConverterCache.Add(memberType, new object[] { ChoType.CreateInstance(type) });
                                        _typeTypeConverterParamsCache.Add(memberType, new object[] { converterAttribute.Parameters });

                                        return(_typeTypeConverterCache[memberType]);
                                    }
                                }

                                TypeConverter converter = TypeDescriptor.GetConverter(memberType);
                                if (converter != null)
                                {
                                    _typeTypeConverterCache.Add(memberType, new object[] { converter });
                                }
                                else
                                {
                                    _typeTypeConverterCache.Add(memberType, EmptyTypeConverters);
                                }

                                _typeTypeConverterParamsCache.Add(memberType, EmptyParams);
                            }

                            return(_typeTypeConverterCache[memberType]);
                        }
                    }

                    return(_typeMemberTypeConverterCache.ContainsKey(memberInfo) ? _typeMemberTypeConverterCache[memberInfo] : EmptyTypeConverters);
                }
            }
        }