public TypeConfig(Type targetType)
        {
            this.TargetType = targetType;
            this.Lifecycle = Lifecycle.PerRequest;

            // inicializa a configuração do tipo para, por omissão, usar o constructor decorado com o atributo DefaultAttribute
            this.ConstructorType = ConstructorType.DefaultConstructor;
        }
Ejemplo n.º 2
0
        public static JobReflectionInfo GetReflectionInfo(Type jobType, Type argsType, string methodName, params Type[] ctorArgs)
        {
            Dictionary <Type, InfoPerArgType> cache;

            lock (CachePerMethod)
            {
                if (!CachePerMethod.TryGetValue(methodName, out cache))
                {
                    cache = new Dictionary <Type, InfoPerArgType>();
                    CachePerMethod.Add(methodName, cache);
                }
            }

            lock (cache)
            {
                JobReflectionInfo reflectionInfo = null;

                if (cache.TryGetValue(jobType, out var argTypeToInfoMap))
                {
                    if (argTypeToInfoMap.TryGetValue(argsType, out reflectionInfo))
                    {
                        return(reflectionInfo);
                    }
                }
                else
                {
                    argTypeToInfoMap = new InfoPerArgType();
                    cache.Add(jobType, argTypeToInfoMap);
                }

                ConstructorInfo c     = null;
                ConstructorType cType = ConstructorType.Other;

                foreach (var ctorArg in ctorArgs)
                {
                    c = jobType.GetConstructor(new[] { ctorArg });

                    if (c != null)
                    {
                        cType = ConstructorType.NeedsPublisher;
                        break;
                    }
                }

                if (c == null)
                {
                    c = jobType.GetConstructor(Type.EmptyTypes);
                    if (c != null)
                    {
                        cType = ConstructorType.NoArgs;
                    }
                }

                if (c == null)
                {
                    var publicCtors = jobType.GetConstructors();
                    if (publicCtors.Length > 0)
                    {
                        c = publicCtors[0];
                    }
                }

                if (c == null)
                {
                    throw new Exception($"Invalid type '{jobType}' for a job. Public constructor not found");
                }

                MethodInfo        m;
                PerformMethodType mType;

                if (argsType == typeof(VoidArgs))
                {
                    mType = PerformMethodType.NeedsCancellationToken;
                    m     = jobType.GetMethod(methodName, new[] { typeof(CancellationToken) });

                    if (m == null)
                    {
                        mType = PerformMethodType.NoArgs;
                        m     = jobType.GetMethod(methodName, Type.EmptyTypes);

                        if (m == null)
                        {
                            throw new Exception($"Invalid type '{jobType}' for a job. {methodName} method with appropriate arguments not found.");
                        }
                    }
                }
                else
                {
                    mType = PerformMethodType.NeedsArgsAndCancellationToken;
                    m     = jobType.GetMethod(methodName, new[] { argsType, typeof(CancellationToken) });

                    if (m == null)
                    {
                        mType = PerformMethodType.NeedsArgs;
                        m     = jobType.GetMethod(methodName, new[] { argsType });

                        if (m == null)
                        {
                            throw new Exception($"Invalid type '{jobType}' for a job. {methodName} method with appropriate arguments not found.");
                        }
                    }
                }

                var useTransProp = jobType.GetProperty("UseTransaction", typeof(bool));

                reflectionInfo = new JobReflectionInfo(c, cType, m, mType, useTransProp);

                argTypeToInfoMap.Add(argsType, reflectionInfo);

                return(reflectionInfo);
            }
        }
Ejemplo n.º 3
0
        public JobReflectionInfo(ConstructorInfo ctor, ConstructorType ctorType, MethodInfo perfMethod,
                                 PerformMethodType perfMethodType, PropertyInfo useTransactionProperty)
        {
            Ctor           = ctor;
            CtorType       = ctorType;
            PerfMethodType = perfMethodType;

            if (useTransactionProperty != null)
            {
                UseTransactionGetter = (Func <object, object>)GetDelegateFactory(
                    nameof(CreateDelegate1),
                    useTransactionProperty.DeclaringType,
                    useTransactionProperty.PropertyType
                    )
                                       .Invoke(null, new object[] { useTransactionProperty.GetMethod });
            }
            else
            {
                UseTransactionGetter = _ => false;
            }

            switch (PerfMethodType)
            {
            case PerformMethodType.NoArgs:
                PerformDelegate1 = (Func <object, object>)GetDelegateFactory(
                    nameof(CreateDelegate1),
                    perfMethod.DeclaringType,
                    perfMethod.ReturnType
                    )
                                   .Invoke(null, new object[] { perfMethod });
                break;

            case PerformMethodType.NeedsArgs:
                PerformDelegate2 = (Func <object, object, object>)GetDelegateFactory(
                    nameof(CreateDelegate2),
                    perfMethod.DeclaringType,
                    perfMethod.GetParameters()[0].ParameterType,
                    perfMethod.ReturnType
                    )
                                   .Invoke(null, new object[] { perfMethod });
                break;

            case PerformMethodType.NeedsCancellationToken:
                PerformDelegate3 = (Func <object, CancellationToken, object>)GetDelegateFactory(
                    nameof(CreateDelegate3),
                    perfMethod.DeclaringType,
                    perfMethod.ReturnType
                    )
                                   .Invoke(null, new object[] { perfMethod });
                break;

            case PerformMethodType.NeedsArgsAndCancellationToken:
                PerformDelegate4 = (Func <object, object, CancellationToken, object>)GetDelegateFactory(
                    nameof(CreateDelegate4),
                    perfMethod.DeclaringType,
                    perfMethod.GetParameters()[0].ParameterType,
                    perfMethod.ReturnType
                    )
                                   .Invoke(null, new object[] { perfMethod });
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(PerfMethodType));
            }
        }
Ejemplo n.º 4
0
 public ConstructorInformation(string className, ConstructorType constructorType, List <ParameterInformation> parameters)
 {
     this.className       = className;
     this.constructorType = constructorType;
     this.Parameters      = parameters;
 }
Ejemplo n.º 5
0
 public ConstructorInformation(string className, ConstructorType constructorType)
 {
     this.className       = className;
     this.constructorType = constructorType;
     this.Parameters      = new List <ParameterInformation>();
 }
Ejemplo n.º 6
0
        public void Unify(TypeVariableReference toUnify, TypeVariableReference toUnifyWith, ITypeUnificationResult unificationResult)
        {
            TypeBase toUnifyTypeBase     = GetTypeForTypeVariableReference(toUnify),
                     toUnifyWithTypeBase = GetTypeForTypeVariableReference(toUnifyWith);

            LiteralType toUnifyLiteral     = toUnifyTypeBase as LiteralType,
                        toUnifyWithLiteral = toUnifyWithTypeBase as LiteralType;

            if (toUnifyLiteral != null && toUnifyWithLiteral != null)
            {
                if (toUnifyLiteral.Type == toUnifyWithLiteral.Type)
                {
                    MergeTypeVariableIntoTypeVariable(toUnify, toUnifyWith);
                    return;
                }
                unificationResult.SetTypeMismatch();
                return;
            }

            ConstructorType toUnifyConstructor     = toUnifyTypeBase as ConstructorType,
                            toUnifyWithConstructor = toUnifyWithTypeBase as ConstructorType;

            if (toUnifyConstructor != null && toUnifyWithConstructor != null)
            {
                if (toUnifyConstructor.ConstructorName == toUnifyWithConstructor.ConstructorName)
                {
                    Unify(toUnifyConstructor.Argument, toUnifyWithConstructor.Argument, unificationResult);
                    MergeTypeVariableIntoTypeVariable(toUnify, toUnifyWith);
                    return;
                }
                unificationResult.SetTypeMismatch();
                return;
            }

            ReferenceType toUnifyReference     = toUnifyTypeBase as ReferenceType,
                          toUnifyWithReference = toUnifyWithTypeBase as ReferenceType;

            if (toUnifyReference != null && toUnifyWithReference != null)
            {
                toUnifyReference.UnifyMutability(toUnifyWithReference);
                Unify(toUnifyReference.UnderlyingType, toUnifyWithReference.UnderlyingType, unificationResult);
                Unify(toUnifyReference.LifetimeType, toUnifyWithReference.LifetimeType, unificationResult);
                return;
            }

            LifetimeTypeContainer toUnifyLifetime     = toUnifyTypeBase as LifetimeTypeContainer,
                                  toUnifyWithLifetime = toUnifyWithTypeBase as LifetimeTypeContainer;

            if (toUnifyLifetime != null && toUnifyWithLifetime != null)
            {
                // toUnify is the possible supertype container here
                toUnifyLifetime.AdoptLifetimeIfPossible(toUnifyWithLifetime.LifetimeValue);
                return;
            }

            TypeVariable toUnifyTypeVariable     = toUnifyTypeBase as TypeVariable,
                         toUnifyWithTypeVariable = toUnifyWithTypeBase as TypeVariable;

            if (toUnifyTypeVariable != null && toUnifyWithTypeVariable != null)
            {
                toUnifyWithTypeVariable.AdoptConstraintsFromVariable(toUnifyTypeVariable);
                MergeTypeVariableIntoTypeVariable(toUnify, toUnifyWith);
                return;
            }
            if (toUnifyTypeVariable != null)
            {
                UnifyTypeVariableWithNonTypeVariable(toUnify, toUnifyWith, unificationResult);
                return;
            }
            if (toUnifyWithTypeVariable != null)
            {
                UnifyTypeVariableWithNonTypeVariable(toUnifyWith, toUnify, unificationResult);
                return;
            }

            unificationResult.SetTypeMismatch();
            return;
        }