Esempio n. 1
0
 private static IEnumerable <object> GetArgumentsForConstructor(ResolvedConstructor constructor)
 {
     // Interface proxy creation requires a null argumentsForConstructor, and null also works
     // for parameterless class constructors, so reduce an empty argument list to null.
     return(constructor.Arguments.Any()
         ? constructor.Arguments.Select(x => x.ResolvedValue)
         : null);
 }
Esempio n. 2
0
            public override CreationResult TryCreateDummyValue(DummyCreationSession session, Type typeOfDummy)
            {
                if (typeof(Delegate).IsAssignableFrom(typeOfDummy))
                {
                    return(CreationResult.FailedToCreateDummy(typeOfDummy, "It is a Delegate."));
                }

                if (typeOfDummy.GetTypeInfo().IsAbstract)
                {
                    return(CreationResult.FailedToCreateDummy(typeOfDummy, "It is abstract."));
                }

                // Save the constructors as we try them. Avoids eager evaluation and double evaluation
                // of constructors enumerable.
                var consideredConstructors = new List <ResolvedConstructor>();

                if (this.cachedConstructors.TryGetValue(typeOfDummy, out ConstructorInfo cachedConstructor))
                {
                    var resolvedConstructor = new ResolvedConstructor(
                        cachedConstructor.GetParameters().Select(pi => pi.ParameterType),
                        session,
                        this.Resolver);
                    if (resolvedConstructor.WasSuccessfullyResolved)
                    {
                        if (TryCreateDummyValueUsingConstructor(cachedConstructor, resolvedConstructor, out object result))
                        {
                            return(CreationResult.SuccessfullyCreated(result));
                        }

                        consideredConstructors.Add(resolvedConstructor);
                    }
                }
                else
                {
                    foreach (var constructor in GetConstructorsInOrder(typeOfDummy))
                    {
                        var resolvedConstructor = new ResolvedConstructor(
                            constructor.GetParameters().Select(pi => pi.ParameterType),
                            session,
                            this.Resolver);

                        if (resolvedConstructor.WasSuccessfullyResolved && TryCreateDummyValueUsingConstructor(constructor, resolvedConstructor, out object result))
                        {
                            this.cachedConstructors[typeOfDummy] = constructor;
                            return(CreationResult.SuccessfullyCreated(result));
                        }

                        consideredConstructors.Add(resolvedConstructor);
                    }
                }

                if (consideredConstructors.Any())
                {
                    return(CreationResult.FailedToCreateDummy(typeOfDummy, consideredConstructors));
                }

                return(CreationResult.FailedToCreateDummy(typeOfDummy, "It has no public constructors."));
            }
Esempio n. 3
0
 private static bool TryCreateDummyValueUsingConstructor(ConstructorInfo constructor, ResolvedConstructor resolvedConstructor, out object result)
 {
     try
     {
         result = constructor.Invoke(resolvedConstructor.Arguments.Select(a => a.ResolvedValue).ToArray());
         return(true);
     }
     catch (TargetInvocationException e)
     {
         result = default;
         resolvedConstructor.ReasonForFailure = e.InnerException.Message;
         return(false);
     }
 }
Esempio n. 4
0
 private static IEnumerable <object?> GetArgumentsForConstructor(ResolvedConstructor constructor) =>
 constructor.Arguments.Select(x => x.ResolvedValue);