Example #1
0
 protected string EmitCreateControl(Type type, object[] arguments)
 {
     // if marked with [RequireDependencyInjection] attribute, invoke injected factory
     if (type.GetTypeInfo().GetCustomAttribute(typeof(DependencyInjection.RequireDependencyInjectionAttribute)) is DependencyInjection.RequireDependencyInjectionAttribute requireDiAttr)
     {
         return(emitter.EmitCustomInjectionFactoryInvocation(requireDiAttr.FactoryType, type));
     }
     // if matching ctor exists, invoke it directly
     else if (type.GetConstructors().FirstOrDefault(ctor =>
                                                    ctor.GetParameters().Count(p => !p.HasDefaultValue) <= (arguments?.Length ?? 0) &&
                                                    ctor.GetParameters().Length >= (arguments?.Length ?? 0) &&
                                                    ctor.GetParameters().Zip(arguments ?? Enumerable.Empty <object>(),
                                                                             (p, a) => TypeConversion.ImplicitConversion(Expression.Constant(a), p.ParameterType))
                                                    .All(a => a != null)) is ConstructorInfo constructor)
     {
         var optionalArguments =
             constructor.GetParameters().Skip(arguments?.Length ?? 0)
             .Select(a =>
                     a.ParameterType == typeof(bool) && a.Name == "allowImplicitLifecycleRequirements" ? false :
                     a.DefaultValue
                     );
         return(emitter.EmitCreateObject(type, arguments == null ? optionalArguments.ToArray() : arguments.Concat(optionalArguments).ToArray()));
     }
     // otherwise invoke DI factory
     else
     {
         return(emitter.EmitInjectionFactoryInvocation(
                    type,
                    (arguments ?? Enumerable.Empty <object>()).Select(a => (a.GetType(), emitter.EmitValue(a))).ToArray(),
                    emitter.InvokeDefaultInjectionFactory
                    ));
     }
 }
Example #2
0
 protected string EmitCreateControl(Type type, object[] arguments)
 {
     // if matching ctor exists, invoke it directly
     if (type.GetTypeInfo().GetCustomAttribute(typeof(DependencyInjection.RequireDependencyInjectionAttribute)) is DependencyInjection.RequireDependencyInjectionAttribute requireDiAttr)
     {
         return(emitter.EmitCustomInjectionFactoryInvocation(requireDiAttr.FactoryType, type));
     }
     else if (type.GetConstructors().Any(ctor =>
                                         ctor.GetParameters().Length == (arguments?.Length ?? 0) &&
                                         ctor.GetParameters().Zip(arguments ?? Enumerable.Empty <object>(),
                                                                  (p, a) => TypeConversion.ImplicitConversion(Expression.Constant(a), p.ParameterType))
                                         .All(a => a != null)))
     {
         return(emitter.EmitCreateObject(type, arguments));
     }
     // othervise invoke DI factory
     else
     {
         return(emitter.EmitInjectionFactoryInvocation(
                    type,
                    (arguments ?? Enumerable.Empty <object>()).Select(a => (a.GetType(), emitter.EmitValue(a))).ToArray(),
                    emitter.InvokeDefaultInjectionFactory
                    ));
     }
 }