public static object InstantiateType(Type type, IArgumentList arglist, out bool isStatic) { isStatic = false; // Sanity check if (type == null) { return(null); } if (type.IsInterface || type.IsAbstract) { return(null); } object o = null; try { // Can we create an instance using Activator.CreateInstance? We can't if it is a singleton // and there is no public constructor. if (type.GetConstructor(Type.EmptyTypes) == null) { isStatic = true; // If we have a public method called ObjectInstance, then use that to get a reference // to the singleton. o = InstantiateSingleton(type); if (o == null) { // As a last resort, find a non-public empty constructor and call it. ConstructorInfo ci = type.GetConstructor( BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic, null, Type.EmptyTypes, null); if (ci != null) { o = ci.Invoke(null, null); } } return(o); } if (arglist != null) { Type[] types = arglist.GetTypes(); ConstructorInfo ci = type.GetConstructor(types); if (ci != null) { o = ci.Invoke(arglist.GetValues()); return(o); } } // We have a default constructor, so construct the object. o = Activator.CreateInstance(type, false); // false == use public constructor } catch (Exception) { // Logger.LogError("Could not instantiate type " + type.Name); } return(o); }