Example #1
0
        internal Type GetInstanceTypeBasedOnTargetGenericConstrains(Type targetType)
        {
            var registeredGenericTypeParameters = new Type[_registeredGenericTypeParameters.Length];

            Array.Copy(_registeredGenericTypeParameters, registeredGenericTypeParameters, _registeredGenericTypeParameters.Length);
            var resolvingTypeDescriptor                      = targetType.Descriptor();
            var resolvingTypeDefinitionDescriptor            = resolvingTypeDescriptor.GetGenericTypeDefinition().Descriptor();
            var resolvingTypeDefinitionGenericTypeParameters = resolvingTypeDefinitionDescriptor.GetGenericTypeParameters();
            var constraintsMap = resolvingTypeDescriptor
                                 .GetGenericTypeArguments()
                                 .Zip(resolvingTypeDefinitionGenericTypeParameters, (type, typeDefinition) => Tuple.Create(type, typeDefinition.Descriptor().GetGenericParameterConstraints()))
                                 .ToArray();

            var canBeResolved = true;

            foreach (var item in _genericParamsWithConstraints)
            {
                var position    = item.Key;
                var descriptor  = item.Value;
                var constraints = descriptor.GetGenericParameterConstraints();

                var isDefined = false;
                foreach (var constraintsEntry in constraintsMap)
                {
                    if (!CoreExtensions.SequenceEqual(constraints, constraintsEntry.Item2))
                    {
                        continue;
                    }

                    registeredGenericTypeParameters[position] = constraintsEntry.Item1;
                    isDefined = true;
                    break;
                }

                if (!isDefined)
                {
                    canBeResolved = false;
                    break;
                }
            }

            return(canBeResolved ? _registeredTypeDescriptor.MakeGenericType(registeredGenericTypeParameters) : null);
        }
Example #2
0
 // ReSharper disable once PossibleNullReferenceException
 public override bool Equals(object obj) =>
 CoreExtensions.SequenceEqual(_genericTypes, ((LifetimeKey)obj)._genericTypes);
Example #3
0
 private Bucket(int count)
 {
     KeyValues = CoreExtensions.CreateArray <KeyValue>(count);
 }
Example #4
0
 public bool Equals(LifetimeKey other) =>
 CoreExtensions.SequenceEqual(_genericTypes, other._genericTypes);
Example #5
0
 private Bucket(KeyValue[] keyValues)
 {
     KeyValues = keyValues.Length == 0 ? CoreExtensions.EmptyArray <KeyValue>() : keyValues.Copy();
 }