private object[] GetConstructorParameters(
            RiskConstraintConfiguration configuration,
            ConstructorInfo constructor)
        {
            var parameters = constructor
                             .GetParameters()
                             .OrderBy(p => p.Position)
                             .ToArray();
            var arguments = new object[parameters.Length];

            for (var i = 0; i < parameters.Length; ++i)
            {
                var parameter = parameters[i];

                object argument;

                if (configuration.Parameters.TryGetValue(parameter.Name, out var argumentStringValue))
                {
                    var converter = TypeDescriptor.GetConverter(parameter.ParameterType);

                    argument = converter.ConvertFromInvariantString(argumentStringValue);
                }
                else
                {
                    argument = _serviceProvider.GetService(parameter.ParameterType);
                }

                arguments[i] = argument;
            }

            return(arguments);
        }
        public IRiskConstraint Create(RiskConstraintConfiguration configuration)
        {
            if (!_constraintImplementationTypes.TryGetValue(configuration.ConstraintName, out var constraintType))
            {
                throw new InvalidOperationException(
                          $"Implementation of risk constraint [{configuration.ConstraintName}] is not found. " +
                          $"Risk constraint implementation should implement {_constraintInterfaceType.FullName}, be placed in the same assembly, " +
                          "be not abstract, not generic, public class with single public constructor without out parameters");
            }

            var constructor = constraintType.GetConstructors(BindingFlags.Instance | BindingFlags.Public).Single();
            var parameters  = GetConstructorParameters(configuration, constructor);
            var constraint  = constructor.Invoke(parameters);

            return((IRiskConstraint)constraint);
        }
Ejemplo n.º 3
0
        private void ConfigureConstraint(
            RiskConstraintsGroupConfiguration groupConfiguration,
            RiskConstraintConfiguration constraintConfiguration)
        {
            var r          = _constraintsRegistry;
            var g          = groupConfiguration;
            var constraint = _riskConstraintsFactory.Create(constraintConfiguration);

            if (g.OperationType.HasValue &&
                !string.IsNullOrWhiteSpace(g.BlockchainType) &&
                !string.IsNullOrWhiteSpace(g.BlockchainAssetId))
            {
                r.Add(g.BlockchainType, g.BlockchainAssetId, g.OperationType.Value, constraint);
            }
            else if (g.OperationType.HasValue &&
                     !string.IsNullOrWhiteSpace(g.BlockchainType))
            {
                r.Add(g.BlockchainType, g.OperationType.Value, constraint);
            }
            else if (g.OperationType.HasValue)
            {
                r.Add(g.OperationType.Value, constraint);
            }
            else if (!string.IsNullOrWhiteSpace(g.BlockchainType) &&
                     !string.IsNullOrWhiteSpace(g.BlockchainAssetId))
            {
                r.Add(g.BlockchainType, g.BlockchainAssetId, constraint);
            }
            else if (!string.IsNullOrWhiteSpace(g.BlockchainType))
            {
                r.Add(g.BlockchainType, constraint);
            }
            else
            {
                r.Add(constraint);
            }
        }