Esempio n. 1
0
        public static HResult CreateInstance(this INiLocalRegistry self, Guid guid, IServiceProvider serviceProvider, out object instance)
        {
            instance = null;

            try
            {
                var hr = self.CreateInstance(guid, out instance);
                if (ErrorUtil.Failure(hr))
                    return hr;

                if (serviceProvider != null)
                {
                    var objectWithSite = instance as INiObjectWithSite;
                    if (objectWithSite != null)
                    {
                        hr = objectWithSite.SetSite(serviceProvider);
                        if (ErrorUtil.Failure(hr))
                            return hr;
                    }
                }

                return HResult.OK;
            }
            catch (Exception ex)
            {
                return ErrorUtil.GetHResult(ex);
            }
        }
        internal static object CreateInstance(this Type type, int arrayLength)
        {
            if (type == null)
                return null;

            return type.CreateInstance(arrayLength, string.Format("Type '{0}' must expose a public parameterless constructor.", type.Name));
        }
Esempio n. 3
0
 public static object CreateInstance(this Type type, List<object> constructorArguments)
 {
     if (type == null) throw new ArgumentNullException("type");
     if (constructorArguments == null) throw new ArgumentNullException("constructorArguments");
     
     return type.CreateInstance(constructorArguments.ToArray());
 }
Esempio n. 4
0
        /// <include file='../_Doc/mscorlib.xml' path='doc/members/member[@name="M:System.Reflection.Assembly.CreateInstance(System.String)"]/*' />
        /// <param name="assembly">Assembly on which the <paramref name="typeName"/> instance should be created.</param>
        public static object CreateInstance(this Assembly assembly, string typeName)
        {
#if DOTNET || WINDOWS_PHONE || WINDOWS_PHONE_APP
            return assembly.CreateInstance(typeName);
#else
            throw new PlatformNotSupportedException("PCL");
#endif
        }
Esempio n. 5
0
        /// <summary>
        /// Creates an object instance.
        /// </summary>
        /// <param name="factory">The target factory.</param>
        /// <param name="serviceType">The requested service type.</param>
        /// <param name="container">The target service contaienr.</param>
        /// <param name="additionalArguments">The additional arguments that will be used to create the service instance.</param>
        /// <returns>A service instance.</returns>
        public static object CreateInstance(this IFactory factory, Type serviceType, 
            IServiceContainer container, params object[] additionalArguments)
        {
            var request = new FactoryRequest()
                              {
                                  ServiceName = null,
                                  ServiceType = serviceType,
                                  Arguments = additionalArguments
                              };

            return factory.CreateInstance(request);
        }
Esempio n. 6
0
        public static Object Reify(this Type t)
        {
            if (t.SameMetadataToken(typeof(IEnumerable<>)) ||
                t.SameMetadataToken(typeof(ICollection<>)) ||
                t.SameMetadataToken(typeof(IList<>)))
            {
                var t_el = t.XGetGenericArguments().AssertSingle();
                t = typeof(List<>).XMakeGenericType(t_el);
            }

            if (t.SameMetadataToken(typeof(IDictionary<,>)))
            {
                var t_key = t.XGetGenericArguments().First();
                if (t_key == typeof(String))
                {
                    var t_value = t.XGetGenericArguments().Second();
                    t = typeof(Dictionary<,>).XMakeGenericType(t_key, t_value);
                }
            }

            if (t.IsInterface || t.IsAbstract)
            {
                var key = typeof(Demiurge).Assembly.ReadKey("XenoGears.XenoGears.snk");
                var unit = Codegen.Units["XenoGears.Demiurge", key];
                t = unit.Context.GetOrCreate(t, () =>
                {
                    var name = String.Format("Reified_{0}", t.Name);
                    var rt = unit.Module.DefineType(name, TA.Public);
                    if (t.IsInterface) t.Hierarchy().ForEach(rt.AddInterfaceImplementation);
                    else rt.SetParent(t);

                    var abstracts = t.Hierarchy().SelectMany(ht => ht.GetProperties(BF.AllInstance)).Where(p => p.IsAbstract()).ToReadOnly();
                    abstracts = abstracts.Where(p1 => abstracts.None(p2 => p2.Overrides(p1))).ToReadOnly();
                    abstracts.ForEach(p =>
                    {
                        MethodBuilder get, set;
                        rt.DefineOverride(p, out get, out set);

                        var f = rt.DefineField("_" + p.Name.Uncapitalize(), p.PropertyType, FA.Private);
                        if (get != null) get.il().ldarg(0).ldfld(f).ret();
                        if (set != null) set.il().ldarg(0).ldarg(1).stfld(f).ret();
                    });

                    return rt.CreateType();
                }).AssertCast<Type>();
            }

            var cfg = t.Config().DefaultEngine().Config;
            var default_ctor = cfg.DefaultCtor && t.HasDefaultCtor();
            return default_ctor ? t.CreateInstance() : t.CreateUninitialized();
        }
Esempio n. 7
0
        /// <summary>
        /// Creates an instance with the provided arguments (if any).
        /// </summary>
        /// <param name="typeInfo">The type information.</param>
        /// <param name="args">The arguments.</param>
        /// <returns>
        /// The new instance.
        /// </returns>
        public static object CreateInstance(this IRuntimeTypeInfo typeInfo, params object[] args)
        {
            Contract.Requires(typeInfo != null);

            return typeInfo.CreateInstance((IEnumerable<object>)args);
        }
 internal static object CreateInstance(this Type type)
 {
     return type.CreateInstance(0);
 }
 internal static object CreateInstance(this PropertyInfo property)
 {
     return property.CreateInstance(0);
 }
Esempio n. 10
0
 public static object GetDefaultValue(this Type type) {
     return type.IsValueType ? type.CreateInstance() : null;
 }
Esempio n. 11
0
        public static object CreateInstanceWithInitializerKeywords(this Type type, List<object> constructorArguments)
        {
            if (type == null) throw new ArgumentNullException("type");
            if (constructorArguments == null) throw new ArgumentNullException("constructorArguments");

            var constructorArgs = new List<object>();
            // Everything up to the first keyword is a constructor arg
            int i;
            for (i = 0; i < constructorArguments.Count; i++)
            {
                var s = constructorArguments[i] as Symbol;
                if (s != null && s.IsKeyword)
                    break;
                constructorArgs.Add(constructorArguments[i]);
            }
            if (((constructorArguments.Count - i) & 1) != 0)
                throw new ArgumentException("Odd number of initializer arguments provided");
            object o = type.CreateInstance(constructorArgs);
            for (; i + 1 < constructorArguments.Count; i += 2)
            {
                var name = constructorArguments[i] as Symbol;
                if (name == null)
                    throw new ArgumentException("Improper initializer name: " + constructorArguments[i]);
                o.SetPropertyOrField(name.KeywordName, constructorArguments[i + 1]);
            }
            return o;
        }
Esempio n. 12
0
 public static object CreateInstance(this Type type, params object[] args)
 {
     return type.CreateInstance(BindingFlags.Public, args);
 }
Esempio n. 13
0
        /// <summary>
        /// Gets an SQL Local DB instance with the specified name if it exists, otherwise a new instance with the specified name is created.
        /// </summary>
        /// <param name="value">The <see cref="ISqlLocalDbProvider"/> to use to get or create the instance.</param>
        /// <param name="instanceName">The name of the SQL Server LocalDB instance to get or create.</param>
        /// <returns>
        /// An SQL Local DB instance with the name specified by <paramref name="instanceName"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="value"/> or <paramref name="instanceName"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// <paramref name="value"/> returns <see langword="null"/> when queried for instances.
        /// </exception>
        public static ISqlLocalDbInstance GetOrCreateInstance(this ISqlLocalDbProvider value, string instanceName)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (instanceName == null)
            {
                throw new ArgumentNullException("instanceName");
            }

            bool instanceExists = false;

            if (SqlLocalDbApi.IsDefaultInstanceName(instanceName))
            {
                // The default instance is always listed, even if it does not exist,
                // so need to query that separately to verify whether to get or create.
                instanceExists = SqlLocalDbApi.GetInstanceInfo(instanceName).Exists;
            }
            else
            {
                // This approach is used otherwise for testability
                IList<ISqlLocalDbInstanceInfo> instances = value.GetInstances();

                if (instances != null)
                {
                    // Instance names in SQL Local DB are case-insensitive
                    instanceExists = instances
                        .Where((p) => p != null)
                        .Where((p) => string.Equals(p.Name, instanceName, StringComparison.OrdinalIgnoreCase))
                        .Any();
                }
            }

            if (instanceExists)
            {
                return value.GetInstance(instanceName);
            }
            else
            {
                return value.CreateInstance(instanceName);
            }
        }