示例#1
0
        public ContextualServiceDescriptor(IServiceCollection services,
                                           Type serviceType, Type implementationType, ServiceLifetime lifetime)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }
            if (implementationType == null)
            {
                throw new ArgumentNullException(nameof(implementationType));
            }
            Services = services ?? throw new ArgumentNullException(nameof(services));

            ServiceDescriptor = new ServiceDescriptor(serviceType, factory, lifetime);

            object factory(IServiceProvider serviceProvider)
            {
                var serviceTypes = ConstructorArguments
                                   .Select(x => x.ServiceType)
                                   .ToArray();
                var arguments = ConstructorArguments
                                .Select(x => serviceProvider.GetService(x.ImplementationType))
                                .ToArray();
                var f = ActivatorUtilities.CreateFactory(implementationType, serviceTypes);

                return(f(serviceProvider, arguments));
            }
        }
示例#2
0
        public Expression BindParameter(ParameterExpression parameter, Dictionary <Expression, Expression> processedExpressions)
        {
            Func <Expression, Expression> genericBinder =
                e => GenericExpressionVisitor <IMappedExpression> .Process(e, mapped => mapped.BindParameter(parameter, processedExpressions));

            return(new ConstructorExpression(
                       Type,
                       Bindings.ToDictionary(kvp => kvp.Key, kvp => genericBinder(kvp.Value)),
                       NativeBindings.ToDictionary(kvp => kvp.Key, kvp => genericBinder(kvp.Value)),
                       Constructor,
                       ConstructorArguments.Select(genericBinder).ToList()));
        }
示例#3
0
        public Expression RemoveOuterParameter(Dictionary <Expression, Expression> processedExpressions)
        {
            Func <Expression, Expression> genericRemover =
                e => GenericExpressionVisitor <IMappedExpression> .Process(e, mapped => mapped.RemoveOuterParameter(processedExpressions));

            var result = new ConstructorExpression(
                Type,
                Bindings.ToDictionary(kvp => kvp.Key, kvp => genericRemover(kvp.Value)),
                NativeBindings = NativeBindings.ToDictionary(kvp => kvp.Key, kvp => genericRemover(kvp.Value)),
                Constructor,
                ConstructorArguments.Select(genericRemover).ToList());

            return(result);
        }
示例#4
0
        public Expression Remap(int[] map, Dictionary <Expression, Expression> processedExpressions)
        {
            Func <IMappedExpression, Expression> remapper = delegate(IMappedExpression mapped) {
                var parametrizedExpression = mapped as ParameterizedExpression;
                if (parametrizedExpression != null && (parametrizedExpression.OuterParameter == OuterParameter || OuterParameter == null))
                {
                    return(mapped.Remap(map, new Dictionary <Expression, Expression>()));
                }
                return((Expression)mapped);
            };
            var newBindings             = Bindings.ToDictionary(kvp => kvp.Key, kvp => GenericExpressionVisitor <IMappedExpression> .Process(kvp.Value, remapper));
            var newConstructorArguments = ConstructorArguments.Select(arg => GenericExpressionVisitor <IMappedExpression> .Process(arg, remapper));
            var newNativeBindings       = NativeBindings.ToDictionary(kvp => kvp.Key, kvp => GenericExpressionVisitor <IMappedExpression> .Process(kvp.Value, remapper));

            return(new ConstructorExpression(Type, newBindings, newNativeBindings, Constructor, newConstructorArguments));
        }
示例#5
0
 public bool IsValid(Notification notification)
 {
     try
     {
         var constructor = GetType().GetConstructor(ConstructorArguments.Select(x => x.GetType()).ToArray());
         if (constructor == null)
         {
             notification.AddError(new ValidationError(string.Format("Failed to extract constructor for operation {0}. Check the order of the parameters going into the base class. The order needs to be the same is in your constructor", GetType().Name)));
             return(false);
         }
     }
     catch (Exception ex)
     {
         notification.AddError(new ValidationError(string.Format("Failed to extract constructor for operation {0}. Error message: {1}", GetType().Name, ex.Message)));
         return(false);
     }
     return(true);
 }
示例#6
0
        public Result Execute(ServerConfig server, ConDepSettings settings, CancellationToken token)
        {
            var assemblyLocalDir  = Path.GetDirectoryName(GetType().Assembly.Location);
            var assemblyRemoteDir = Path.Combine(server.GetServerInfo().TempFolderDos, "Assemblies");

            var publisher = new FilePublisher();

            publisher.PublishDirectory(assemblyLocalDir, assemblyRemoteDir, server, settings);

            var remoteAssemblyFileName = Path.Combine(Path.Combine(server.GetServerInfo().TempFolderPowerShell, "Assemblies"),
                                                      Path.GetFileName(GetType().Assembly.Location));
            var remoteJsonAssembly = Path.Combine(Path.Combine(server.GetServerInfo().TempFolderPowerShell, "Assemblies"),
                                                  "Newtonsoft.Json.dll");
            var typeName       = GetType().FullName;
            var loggerTypeName = typeof(RemotePowerShellLogger).FullName;

            var parameters   = GetPowerShellParameters(ConstructorArguments, GetType()).ToList();
            var scriptParams = string.Join(",", parameters.Select(x => "$" + x.Name));
            var argumentList = string.Join(",",
                                           GetType().GetConstructor(ConstructorArguments.Select(x => x.GetType()).ToArray()).GetParameters()
                                           .Select(x => "$" + x.Name));
            var deserializeScript = GetDeserializationScript(GetType()
                                                             .GetConstructor(ConstructorArguments.Select(x => x.GetType()).ToArray()));

            var psExecutor = new PowerShellExecutor();
            var script     = string.Format(@"
Param({3})
add-type -path {0}
add-type -path {5}
{4}
$operation = new-object -typename {1} -ArgumentList {6}
$logger = new-object -typename {2} -ArgumentList (Get-Host).UI
$operation.Execute($logger)
", remoteAssemblyFileName, typeName, loggerTypeName, scriptParams, deserializeScript, remoteJsonAssembly,
                                           argumentList);

            psExecutor.Execute(server, script, null, parameters);
            return(Result.SuccessChanged());
        }
 public override string ToString()
 {
     return($"{AttributeType.FullName}-({string.Join(",",ConstructorArguments.Select(_ => _.Value))})");
 }