Exemple #1
0
        public void IsParams()
        {
            ParameterDefinition pd = null;

            Assert.IsFalse(pd.IsParams(), "null");

            MethodDefinition md = type.GetMethod("UseParams");

            Assert.IsFalse(md.Parameters [0].IsParams(), "0");
            Assert.IsTrue(md.Parameters [1].IsParams(), "1");
        }
Exemple #2
0
        static bool MatchParameters(Collection <ParameterDefinition> pmethod, Collection <ParameterDefinition> candidate, int offset)
        {
            int ccount = candidate.Count - offset;
            int count  = Math.Min(pmethod.Count, ccount - offset);

            for (int i = 0; i < count; i++)
            {
                ParameterDefinition pd = candidate [i + offset];
                if (pd.IsParams())
                {
                    return(true);
                }
                TypeReference ptype = pd.ParameterType;
                if (!pmethod [i].ParameterType.IsNamed(ptype.Namespace, ptype.Name))
                {
                    return(false);
                }
            }
            return(ccount - count <= 1);
        }
Exemple #3
0
        /// <summary>
        /// Get method signature of invoked method. It will be resolved with
        /// generic arguments while the method is invoked.
        /// </summary>
        /// <param name="method">Method to get resolved signature.</param>
        /// <param name="methodDef">Method definition of method parameter.</param>
        /// <returns>
        /// Method signature with generic parameters resolved, e.g., List&lt;string&gt;
        /// Generic parameters are resolved to argument values.
        /// </returns>
        public static string GetResolvedMethodSignature(this MethodReference method, MethodDefinition methodDef)
        {
            string typeSignature = GetResolvedTypeSignature(method.DeclaringType);
            string methodSignature;

            if (method.Name.StartsWith(GetPrefix))
            {
                methodSignature = $"{method.Name.Substring(GetPrefix.Length)}.get";
            }
            else if (method.Name.StartsWith(SetPrefix))
            {
                methodSignature = $"{method.Name.Substring(SetPrefix.Length)}.set";
            }
            else
            {
                methodSignature = method.Name;
            }

            if (method.IsGenericInstance)
            {
                GenericInstanceMethod genericInstance = (GenericInstanceMethod)method;
                var genericParameters = genericInstance.GenericArguments.Select(genericArgument => genericArgument.GetResolvedTypeSignature());
                methodSignature = genericParameters.Count() != 0 ? $"{methodSignature}<{string.Join(",", genericParameters)}>" : methodSignature;
            }

            IList <string> parameters = new List <string>();

            if (method.IsGenericInstance)
            {
                GenericInstanceMethod genericInstanceMethod = (GenericInstanceMethod)method;
                for (int i = 0; i < method.Parameters.Count; i++)
                {
                    ParameterDefinition parameter             = method.Parameters[i];
                    ParameterDefinition parameterOfMethodDef  = methodDef.Parameters[i];
                    TypeReference       resolvedParameterType = parameter.GetTypeWithGenericResolved(genericInstanceMethod);
                    if (resolvedParameterType.IsByReference)
                    {
                        ByReferenceType byReference = resolvedParameterType as ByReferenceType;
                        string          prefix;
                        if (parameterOfMethodDef.IsOut)
                        {
                            prefix = "out";
                        }
                        else if (parameterOfMethodDef.IsIn)
                        {
                            prefix = "in";
                        }
                        else
                        {
                            prefix = "ref";
                        }

                        parameters.Add($"{prefix}{byReference.ElementType.GetResolvedTypeSignature()}");
                    }
                    else if (parameterOfMethodDef.IsParams())
                    {
                        parameters.Add($"params{resolvedParameterType.GetResolvedTypeSignature()}");
                    }
                    else
                    {
                        parameters.Add(resolvedParameterType.GetResolvedTypeSignature());
                    }
                }
            }
            else
            {
                for (int i = 0; i < method.Parameters.Count; i++)
                {
                    ParameterDefinition parameter             = method.Parameters[i];
                    ParameterDefinition parameterOfMethodDef  = methodDef.Parameters[i];
                    TypeReference       resolvedParameterType = parameter.GetTypeWithGenericResolved();
                    if (resolvedParameterType.IsByReference)
                    {
                        ByReferenceType byReference = resolvedParameterType as ByReferenceType;
                        string          prefix;
                        if (parameterOfMethodDef.IsOut)
                        {
                            prefix = "out";
                        }
                        else if (parameterOfMethodDef.IsIn)
                        {
                            prefix = "in";
                        }
                        else
                        {
                            prefix = "ref";
                        }

                        parameters.Add($"{prefix}{byReference.ElementType.GetResolvedTypeSignature()}");
                    }
                    else if (parameterOfMethodDef.IsParams())
                    {
                        parameters.Add($"params{resolvedParameterType.GetResolvedTypeSignature()}");
                    }
                    else
                    {
                        parameters.Add(resolvedParameterType.GetResolvedTypeSignature());
                    }
                }
            }

            return($"{typeSignature}.{methodSignature}({string.Join(",", parameters)})");
        }