Exemplo n.º 1
0
 public static InjectTypeInfo.InjectConstructorInfo ConvertConstructor(
     ReflectionTypeInfo.InjectConstructorInfo injectConstructor, Type type)
 {
     return(new InjectTypeInfo.InjectConstructorInfo(
                TryCreateFactoryMethod(type, injectConstructor),
                injectConstructor.Parameters.Select(x => x.InjectableInfo).ToArray()));
 }
Exemplo n.º 2
0
        static UniDiFactoryMethod TryCreateFactoryMethod(
            Type type, ReflectionTypeInfo.InjectConstructorInfo reflectionInfo)
        {
#if !NOT_UNITY3D
            if (type.DerivesFromOrEqual <Component>())
            {
                return(null);
            }
#endif

            if (type.IsAbstract())
            {
                Assert.That(reflectionInfo.Parameters.IsEmpty());
                return(null);
            }

            var constructor = reflectionInfo.ConstructorInfo;

            var factoryMethod = TryCreateFactoryMethodCompiledLambdaExpression(type, constructor);

            if (factoryMethod == null)
            {
                if (constructor == null)
                {
                    if (ReflectionTypeAnalyzer.ConstructorChoiceStrategy == ConstructorChoiceStrategy.InjectAttribute)
                    {
                        return(null);
                    }

                    // No choice in this case except to use the slow Activator.CreateInstance
                    // as far as I know
                    // This should be rare though and only seems to occur when instantiating
                    // structs on platforms that don't support lambda expressions
                    // Non-structs should always have a default constructor
                    factoryMethod = args =>
                    {
                        Assert.That(args.Length == 0);
                        return(Activator.CreateInstance(type, new object[0]));
                    };
                }
                else
                {
                    factoryMethod = constructor.Invoke;
                }
            }

            return(factoryMethod);
        }