Exemplo n.º 1
0
        /// <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>
        public static T GetInstance(SingletonTypeValidationRules singletonTypeValidationRules, params object[] args)
        {
            if (_instance != null)
            {
                return(_instance);
            }

            lock (_padLock)
            {
                if (_instance != null)
                {
                    return(_instance);
                }

                _instance     = new ChoSingletonHelper <T>().GetInstance(false, args, singletonTypeValidationRules);
                _isInitizlied = true;

                return(_instance);
            }
        }
Exemplo n.º 2
0
        /// <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 GetInstance(bool strict, object[] args, SingletonTypeValidationRules singletonTypeValidationRules)
        {
            T _instance = default(T);

            VerifySingtonType(strict, singletonTypeValidationRules);

            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(String.Format("[Type: {0}] Instance object is null.", typeof(T).FullName));
            }

            return(_instance);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Helper method to verify the singleton type
        /// </summary>
        private void VerifySingtonType(bool strict, SingletonTypeValidationRules singletonTypeValidationRules)
        {
            //Make sure the type is sealed
            if (((singletonTypeValidationRules & SingletonTypeValidationRules.AllowNotSealedType) != SingletonTypeValidationRules.AllowNotSealedType) &&
                !typeof(T).IsSealed)
            {
                throw new ChoSingletonException(String.Format("The '{0}' type must be sealed class.", typeof(T).FullName));
            }

            if (((singletonTypeValidationRules & SingletonTypeValidationRules.AllowStaticMembers) != SingletonTypeValidationRules.AllowStaticMembers))
            {
                //Make sure there is no static members other than self create instance
                MemberInfo[] memberInfos = typeof(T).GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                if (memberInfos.Length > 0)
                {
                    foreach (MemberInfo memberInfo in memberInfos)
                    {
                        if (memberInfo.MemberType == MemberTypes.Method &&
                            memberInfo.GetCustomAttributes(typeof(ChoSingletonFactoryMethodAttribute), false).Length > 0)
                        {
                            continue;
                        }

                        throw new ChoSingletonException(String.Format("The '{0}' type must not have STATIC [{1}] members other than self create instance STATIC method.", typeof(T).FullName,
                                                                      memberInfo.ToString()));
                    }
                }
            }

            ConstructorInfo[] constructorInfos = typeof(T).GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            if (((singletonTypeValidationRules & SingletonTypeValidationRules.AllowPublicConstructors) != SingletonTypeValidationRules.AllowPublicConstructors))
            {
                if (strict)
                {
                    foreach (ConstructorInfo constructorInfo in constructorInfos)
                    {
                        if (!constructorInfo.IsPrivate)
                        {
                            throw new ChoSingletonException(String.Format("One of constructor [{0}] in '{1}' type has modifier specified other than private. All the constructors must be private.",
                                                                          constructorInfo.ToString(), typeof(T).FullName));
                        }
                    }
                }
                else
                {
                    foreach (ConstructorInfo constructorInfo in constructorInfos)
                    {
                        if (!constructorInfo.IsPrivate && !constructorInfo.IsFamily)
                        {
                            throw new ChoSingletonException(String.Format("One of constructor [{0}] in '{1}' has modifier specified other than private / protected. All the constructors must be private/protected.",
                                                                          constructorInfo.ToString(), typeof(T).FullName));
                        }
                    }
                }
            }

            //Make sure the type have only one constructor
            if (constructorInfos.Length > 1)
            {
                if (typeof(T).GetCustomAttributes(typeof(ChoCustomSingletonTypeAttribute), false).Length == 1)
                {
                    if (GetSingletonContructor() != null)
                    {
                        return;
                    }
                }
                if (!HasFactoryMethodDefined())
                {
                    throw new ChoSingletonException(String.Format("The '{0}' type must have only one private constructor. Or decorate a constructor with ChoSingetonConstructorAttribute.", typeof(T).FullName));
                }
            }
        }
Exemplo n.º 4
0
 public static void Initialize(SingletonTypeValidationRules singletonTypeValidationRules, params object[] args)
 {
     T obj = GetInstance(singletonTypeValidationRules, args);
 }