Beispiel #1
0
 public void Init(string constructorArgs)
 {
     if (String.IsNullOrEmpty(constructorArgs))
     {
         _constructorArgs      = new object[0];
         _constructorArgsTypes = new Type[0];
     }
     else
     {
         _constructorArgs      = ChoString.Split2Objects(constructorArgs);
         _constructorArgsTypes = ChoType.ConvertToTypes(_constructorArgs);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Helper method to call self create instance method.
        /// </summary>
        /// <param name="args">List of parameters required by self create instance method.</param>
        /// <returns>Instance of the class return by self create instance method.</returns>
        private T CreateInstance(object[] args)
        {
            foreach (MethodInfo methodInfo in typeof(T).GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
            {
                if (methodInfo.GetCustomAttributes(typeof(ChoSingletonFactoryMethodAttribute), false).Length == 0)
                {
                    continue;
                }

                if (methodInfo.IsStatic)
                {
                    if (methodInfo.IsConstructor || methodInfo.IsGenericMethod || methodInfo.IsGenericMethodDefinition &&
                        (typeof(T) != methodInfo.ReturnType || !typeof(T).IsSubclassOf(methodInfo.ReturnType)))
                    {
                        throw new ChoSingletonException(String.Format("Invalid Singleton Self Create Instance Method found in '{0}' type.",
                                                                      typeof(T).FullName));
                    }

                    if (methodInfo.GetParameters().Length == 0)
                    {
                        return(methodInfo.Invoke(null, null) as T);
                    }
                    else
                    {
                        try
                        {
                            return(methodInfo.Invoke(null, args) as T);
                        }
                        catch (Exception innerEx)
                        {
                            throw new ChoSingletonException(String.Format("Can't find self create instance method with matching parameters [{0}] in '{1}' type.",
                                                                          ChoString.Join(ChoType.ConvertToTypes(args))), innerEx);
                        }
                    }
                }
                break;
            }

            throw new ChoSingletonException("Missing Singleton Self Create Instance Method. It should be STATIC method.");
        }