Ejemplo n.º 1
0
        public static TypeReference GetTypeWithGenericResolved(this ParameterDefinition definition, GenericInstanceMethod genericInstanceMethod)
        {
            var  typeWithGenericResolved = definition.ParameterType;
            bool isByReference           = false;
            bool isArray   = false;
            int  arrayRank = 0;

            if (typeWithGenericResolved.IsByReference)
            {
                isByReference = true;
                ByReferenceType byReferenceType = (ByReferenceType)typeWithGenericResolved;
                typeWithGenericResolved = byReferenceType.ElementType;
            }

            if (typeWithGenericResolved.IsArray)
            {
                isArray = true;
                ArrayType arrayType = (ArrayType)typeWithGenericResolved;
                arrayRank = arrayType.Rank;
                typeWithGenericResolved = arrayType.ElementType;
            }

            if (typeWithGenericResolved.IsGenericInstance)
            {
                GenericInstanceType genericInstanceType = (GenericInstanceType)typeWithGenericResolved;
                typeWithGenericResolved = genericInstanceType.GetTypeWithGenericResolved(((MethodReference)definition.Method).DeclaringType as GenericInstanceType, genericInstanceMethod);
            }

            if (typeWithGenericResolved.IsGenericParameter)
            {
                GenericParameter genericParam = typeWithGenericResolved as GenericParameter;
                var declaringMethod           = (MethodReference)definition.Method;
                if (genericParam.DeclaringType != null && declaringMethod.DeclaringType is GenericInstanceType genericInstanceTypeOfParam)
                {
                    var position = genericInstanceTypeOfParam.ElementType.GenericParameters.GetIndexByName(typeWithGenericResolved.Name);
                    if (position != -1)
                    {
                        typeWithGenericResolved = genericInstanceTypeOfParam.GenericArguments[position];
                    }
                }
                else if (genericParam.DeclaringMethod != null && genericInstanceMethod != null)
                {
                    var position = declaringMethod.GetElementMethod().GenericParameters.GetIndexByName(typeWithGenericResolved.Name);
                    if (position != -1)
                    {
                        typeWithGenericResolved = genericInstanceMethod.GenericArguments[position];
                    }
                }
            }

            if (isArray)
            {
                typeWithGenericResolved = new ArrayType(typeWithGenericResolved, arrayRank);
            }

            if (isByReference)
            {
                typeWithGenericResolved = new ByReferenceType(typeWithGenericResolved);
            }

            return(typeWithGenericResolved);
        }