Ejemplo n.º 1
0
        internal static T CreateInstance <T>(Type objType, ChoObjectConstructionType defaultObjectConstructionType,
                                             bool beforeFieldInit)
        {
            Exception ex        = null;
            T         retObject = CreateInstance <T>(objType, defaultObjectConstructionType, beforeFieldInit, out ex);

            if (ex != null)
            {
                throw new ChoApplicationException("Failed to create object.", ex);
            }
            else
            {
                return(retObject);
            }
        }
Ejemplo n.º 2
0
 public ChoObjectFactoryAttribute(ChoObjectConstructionType objectConstructionType)
 {
     _objectConstructionType = objectConstructionType;
 }
Ejemplo n.º 3
0
        internal static T CreateInstance <T>(Type objType, ChoObjectConstructionType defaultObjectConstructionType,
                                             bool beforeFieldInit, out Exception exception)
        {
            exception = null;

            ChoGuard.ArgumentNotNull(objType, "objType");

            T instance = default(T);
            ChoObjectConstructionType objectConstructionType = defaultObjectConstructionType;

            if (IsConstructing(objType))
            {
                instance = New <T>(objType, null);
                if (instance != null)
                {
                    ChoObjectInitializer.Initialize(instance, beforeFieldInit);
                }
            }
            else
            {
                try
                {
                    _globalObjectBuildStateCache[objType] = ObjectBuildState.Constructing;
                    IChoCustomObjectFactory   customObjectFactory    = null;
                    ChoObjectFactoryAttribute objectFactoryAttribute = ChoType.GetAttribute(objType, typeof(ChoObjectFactoryAttribute)) as ChoObjectFactoryAttribute;

                    if (objectFactoryAttribute != null)
                    {
                        objectConstructionType = objectFactoryAttribute.ObjectConstructionType;
                        customObjectFactory    = objectFactoryAttribute.CustomObjectFactory;
                    }

                    switch (objectConstructionType)
                    {
                    case ChoObjectConstructionType.Singleton:
                        //lock (_globalObjectCache.SyncRoot)
                        //{
                        if (_globalObjectCache.ContainsKey(objType))
                        {
                            return((T)_globalObjectCache[objType]);
                        }
                        else
                        {
                            instance = New <T>(objType, customObjectFactory);
                            _globalObjectCache[objType] = instance;
                        }
                        //}
                        break;

                    default:
                        instance = New <T>(objType, customObjectFactory);
                        break;
                    }

                    if (instance != null)
                    {
                        try
                        {
                            ChoObjectInitializer.Initialize(instance, beforeFieldInit);
                        }
                        catch (TypeInitializationException)
                        {
                            throw;
                        }
                        catch (ChoFatalApplicationException)
                        {
                            throw;
                        }
                        catch (Exception ex)
                        {
                            exception = ex;
                        }
                    }
                }
                finally
                {
                    _globalObjectBuildStateCache[objType] = ObjectBuildState.Constructed;
                }
            }

            return(instance);
        }
Ejemplo n.º 4
0
 internal static T CreateInstance <T>(ChoObjectConstructionType defaultObjectConstructionType,
                                      bool beforeFieldInit)
 {
     return(CreateInstance <T>(typeof(T), defaultObjectConstructionType, beforeFieldInit));
 }
Ejemplo n.º 5
0
 internal static object CreateInstance(Type objType, ChoObjectConstructionType defaultObjectConstructionType,
                                       bool beforeFieldInit, out Exception exception)
 {
     return(CreateInstance <object>(objType, defaultObjectConstructionType, beforeFieldInit, out exception));
 }
Ejemplo n.º 6
0
 public static T CreateInstance <T>(Type objType, ChoObjectConstructionType defaultObjectConstructionType)
 {
     return(CreateInstance <T>(objType, defaultObjectConstructionType, false));
 }
Ejemplo n.º 7
0
 public static T CreateInstance <T>(ChoObjectConstructionType defaultObjectConstructionType)
 {
     return(CreateInstance <T>(typeof(T), defaultObjectConstructionType, false));
 }
Ejemplo n.º 8
0
 internal static T CreateInstance <T>(string typeName, ChoObjectConstructionType defaultObjectConstructionType,
                                      bool beforeFieldInit)
 {
     return(CreateInstance <T>(ResolveType(typeName), defaultObjectConstructionType, beforeFieldInit));
 }
Ejemplo n.º 9
0
 public static T CreateInstance <T>(string typeName, ChoObjectConstructionType defaultObjectConstructionType)
 {
     return(CreateInstance <T>(typeName, defaultObjectConstructionType, false));
 }