public void When_instantiating_class_with_ctor_arguments_then_the_needed_objects_get_created()
        {
            var ctx = new CompositionContext();

            ctx.AddPart <ExportClass1, ExportClass1>();
            ctx.AddPart <ExportClassWithCtor, ExportClassWithCtor>();

            var part = ctx.GetPart <ExportClassWithCtor>();

            Assert.IsNotNull(part.Import);
            Assert.AreEqual(part.Import, ctx.GetPart <ExportClass1>());
        }
Beispiel #2
0
        private object CreatePart(CompositionContext ctx)
        {
#if !LEGACY
            var constructor = Type
                .GetTypeInfo()
                .DeclaredConstructors
                .First();
#else
            var constructor = Type
                .GetConstructors()
                .First();
#endif

            var parameters = constructor
                .GetParameters()
                .Select(p => ctx.GetPart(p.ParameterType, null))
                .ToArray();

            var part = constructor.Invoke(parameters);
            return part;
        }
        public void When_instantiating_class_with_ctor_arguments_then_the_needed_objects_get_created()
        {
            var ctx = new CompositionContext();

            ctx.AddPart<ExportClass1, ExportClass1>();
            ctx.AddPart<ExportClassWithCtor, ExportClassWithCtor>();

            var part = ctx.GetPart<ExportClassWithCtor>();

            Assert.IsNotNull(part.Import);
            Assert.AreEqual(part.Import, ctx.GetPart<ExportClass1>());
        }
Beispiel #4
0
 /// <summary>Returns a service object. </summary>
 /// <typeparam name="TInterface">The interface type of the service. </typeparam>
 /// <returns>The service object. </returns>
 public TInterface Resolve <TInterface>()
 {
     return(_context.GetPart <TInterface>());
 }