コード例 #1
0
        private static object Create(Cat cat, Type type, Type[] genericArguments)
        {
            if (genericArguments.Length > 0)
            {
                type = type.MakeGenericType(genericArguments);
            }

            //var constrcotrs = type.GetConstructors(System.Reflection.BindingFlags.Instance);
            var constrcotrs = type.GetConstructors();

            if (constrcotrs.Length == 0)
            {
                throw new InvalidOperationException($"Cannot create the instance of {type} which does not have an public constructor.");
            }

            var constrcotor = constrcotrs.FirstOrDefault(it => it.GetCustomAttributes(false).OfType <InjectionAttribute>().Any());

            constrcotor = constrcotor ?? constrcotrs.First();
            var parameters = constrcotor.GetParameters();

            if (parameters.Length == 0)
            {
                return(Activator.CreateInstance(type));
            }

            var arguments = new object[parameters.Length];

            for (int index = 0; index < arguments.Length; index++)
            {
                var parameter     = parameters[index];
                var parameterType = parameter.ParameterType;
                if (cat.HasRegistry(parameterType))
                {
                    arguments[index] = cat.GetService(parameterType);
                }
                else if (parameter.HasDefaultValue)
                {
                    arguments[index] = parameter.DefaultValue;
                }
                else
                {
                    throw new InvalidOperationException($"Cannot create the instance of {type} whose constructor has non-registered parameter type(s)");
                }
            }

            return(Activator.CreateInstance(type, arguments));
        }
コード例 #2
0
 public static T GetService <T>(this Cat cat) => (T)cat.GetService(typeof(T));
コード例 #3
0
 public static IEnumerable <T> GetServices <T>(this Cat cat) => cat.GetService <IEnumerable <T> >();
コード例 #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var a = typeof(Foo).GetConstructors();

            Console.WriteLine(1);

            var root = new Cat()
                       .Register <IFoo, Foo>(Lifetime.Transient)
                       .Register <IBar>(_ => new Bar(), Lifetime.Self)
                       .Register <IBaz, Baz>(Lifetime.Root);

            var cat1 = root.CreateChild();
            var cat2 = root.CreateChild();

            void GetServices <TService>(Cat cat)
            {
                cat.GetService <TService>();
                cat.GetService <TService>();
            }

            GetServices <IFoo>(cat1);
            GetServices <IBar>(cat1);
            GetServices <IBaz>(cat1);
            Console.WriteLine();


            GetServices <IFoo>(cat2);
            GetServices <IBar>(cat2);
            GetServices <IBaz>(cat2);

            //
            Console.WriteLine(2);
            var myNewCat = new Cat()
                           .Register <IFoo, Foo>(Lifetime.Transient)
                           .Register <IBar, Bar>(Lifetime.Transient)
                           .Register(typeof(IFoobar <,>), typeof(FooBar <,>), Lifetime.Transient);
            var foobar = (FooBar <IFoo, IBar>)myNewCat.GetService <IFoobar <IFoo, IBar> >();

            Debug.Assert(foobar.Foo is Foo);
            Debug.Assert(foobar.Bar is Bar);


            //
            Console.WriteLine(3);

            var services = new Cat().Register <Base, Foo>(Lifetime.Transient)
                           .Register <Base, Bar>(Lifetime.Transient)
                           .Register <Base, Baz>(Lifetime.Transient)
                           .GetServices <Base>();

            Debug.Assert(services.OfType <Foo>().Any());
            Debug.Assert(services.OfType <Bar>().Any());
            Debug.Assert(services.OfType <Baz>().Any());

            Console.WriteLine(4);

            using (var anotherRoot = new Cat().Register <IFoo, Foo>(Lifetime.Transient)
                                     .Register <IBar, Bar>(Lifetime.Self)
                                     .Register <IBaz, Baz>(Lifetime.Root))
            {
                using (var cat = anotherRoot.CreateChild())
                {
                    cat.GetService <IFoo>();
                    cat.GetService <IBar>();
                    cat.GetService <IBaz>();
                }
                Console.WriteLine("Child cat is disposed.");
            }
            Console.WriteLine("Root cat is disposed.");

            Console.WriteLine(".........................Test DI..............");
            TestDI();

            TestDI_Dispose();
        }