private static MethodReference clone_opr_gmr(object osrc)
        {
            MethodReference src = (MethodReference)osrc;

            if (src.IsGenericInstance)
            {
                GenericInstanceMethod g_src = (GenericInstanceMethod)src;
                src = g_src.GetElementMethod();
                MethodReference       dst   = clone_opr_mr(src);
                GenericInstanceMethod g_dst = new GenericInstanceMethod(dst);
                foreach (TypeReference par in g_src.GenericArguments)
                {
                    g_dst.GenericArguments.Add(par);
                }
                return(g_dst);
            }
            else
            {
                return(clone_opr_mr(src));
            }
        }
Example #2
0
        internal static TypeReference GetTypeWithGenericResolved(
            this GenericInstanceType genericInstanceParameter,
            GenericInstanceType genericInstanceType,
            GenericInstanceMethod genericInstanceMethod)
        {
            GenericInstanceType newGenericInstanceType = new GenericInstanceType(genericInstanceParameter.ElementType);

            foreach (var genericArg in genericInstanceParameter.GenericArguments)
            {
                TypeReference newGenericArg = genericArg;
                bool          isByReference = false;
                bool          isArray       = false;
                int           arrayRank     = 0;
                if (newGenericArg.IsByReference)
                {
                    isByReference = true;
                    ByReferenceType byReferenceType = (ByReferenceType)newGenericArg;
                    newGenericArg = byReferenceType.ElementType;
                }

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

                if (newGenericArg.IsGenericInstance)
                {
                    newGenericArg = ((GenericInstanceType)newGenericArg).GetTypeWithGenericResolved(genericInstanceType, genericInstanceMethod);
                }

                if (newGenericArg.IsGenericParameter)
                {
                    GenericParameter tmp = newGenericArg as GenericParameter;
                    if (tmp.DeclaringType != null && genericInstanceType != null)
                    {
                        int position = genericInstanceType.ElementType.GenericParameters.GetIndexByName(newGenericArg.Name);
                        if (position != -1)
                        {
                            newGenericArg = genericInstanceType.GenericArguments[position];
                        }
                    }
                    else if (tmp.DeclaringMethod != null && genericInstanceMethod != null)
                    {
                        int position = genericInstanceMethod.GetElementMethod().GenericParameters.GetIndexByName(newGenericArg.Name);
                        if (position != -1)
                        {
                            newGenericArg = genericInstanceMethod.GenericArguments[position];
                        }
                    }
                }

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

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

                newGenericInstanceType.GenericArguments.Add(newGenericArg);
            }

            return(newGenericInstanceType);
        }