Esempio n. 1
0
        string[] _getTypeNames(System.Type typeBase, string[] assemblyNames)
        {
            List <string> typeNames = new List <string>();

            foreach (string assemblyName in assemblyNames)
            {
                Assembly assembly = null;
                try
                {
                    assembly = Assembly.Load(assemblyName);
                }
                catch
                {
                    continue;
                }

                if (assembly == null)
                {
                    continue;
                }

                System.Type[] types = assembly.GetTypes();
                foreach (System.Type type in types)
                {
                    if (type.IsClass && !type.IsAbstract && typeBase.IsAssignableFrom(type))
                    {
                        typeNames.Add(type.FullName);
                    }
                }
            }

            typeNames.Sort();
            return(typeNames.ToArray());
        }
Esempio n. 2
0
        public static void Register(System.Type tp, IService service)
        {
            if (tp == null)
            {
                throw new System.ArgumentNullException("tp");
            }
            if (service.IsNullOrDestroyed())
            {
                throw new System.ArgumentNullException("service");
            }
            if (!typeof(IService).IsAssignableFrom(tp))
            {
                throw new System.ArgumentException("Type must implement IService.", "tp");
            }
            if (!tp.IsAssignableFrom(service.GetType()))
            {
                throw new System.ArgumentException(string.Format("Service must implement the service type '{0}' it's being registered for.", tp.Name), "tp");
            }

            System.Reflection.FieldInfo field = GetStaticEntryFieldInfo(tp, true);
            if (field == null)
            {
                throw new System.InvalidOperationException("Failed to resolve type '" + tp.Name + "'.");
            }

            var other = field.GetValue(null);

            if (!other.IsNullOrDestroyed() && !object.ReferenceEquals(other, service))
            {
                throw new System.InvalidOperationException("You must first unregister a service before registering a new one.");
            }

            _services.Add(service);
            field.SetValue(null, service);
            _serviceTable[tp] = service;

            //make sure we don't add it multiple times
            service.Disposed -= OnServiceInadvertentlyDisposed;
            service.Disposed += OnServiceInadvertentlyDisposed;
        }