Ejemplo n.º 1
0
        public IServerFactory GetServerFactory(string serverFactoryIdentifier)
        {
            if (string.IsNullOrEmpty(serverFactoryIdentifier))
            {
                throw new ArgumentException(string.Empty, "serverFactoryIdentifier");
            }

            var    nameParts    = HostingUtilities.SplitTypeName(serverFactoryIdentifier);
            string typeName     = nameParts.Item1;
            string assemblyName = nameParts.Item2;

            var assembly = Assembly.Load(new AssemblyName(assemblyName));

            if (assembly == null)
            {
                throw new Exception(String.Format("TODO: assembly {0} failed to load message", assemblyName));
            }

            Type type = null;
            Type interfaceInfo;

            if (string.IsNullOrEmpty(typeName))
            {
                foreach (var typeInfo in assembly.DefinedTypes)
                {
                    interfaceInfo = typeInfo.ImplementedInterfaces.FirstOrDefault(interf => interf.Equals(typeof(IServerFactory)));
                    if (interfaceInfo != null)
                    {
                        type = typeInfo.AsType();
                    }
                }

                if (type == null)
                {
                    throw new Exception(String.Format("TODO: type {0} failed to load message", typeName ?? "<null>"));
                }
            }
            else
            {
                type = assembly.GetType(typeName);

                if (type == null)
                {
                    throw new Exception(String.Format("TODO: type {0} failed to load message", typeName ?? "<null>"));
                }

                interfaceInfo = type.GetTypeInfo().ImplementedInterfaces.FirstOrDefault(interf => interf.Equals(typeof(IServerFactory)));

                if (interfaceInfo == null)
                {
                    throw new Exception("TODO: IServerFactory interface not found");
                }
            }

            return((IServerFactory)ActivatorUtilities.GetServiceOrCreateInstance(_services, type));
        }