IsEligibleExtensionMethod() static private method

static private IsEligibleExtensionMethod ( ICompilation compilation, CSharpConversions conversions, IType targetType, IMethod method, bool useTypeInference, IType &outInferredTypes ) : bool
compilation ICompilation
conversions CSharpConversions
targetType IType
method IMethod
useTypeInference bool
outInferredTypes IType
return bool
        /// <summary>
        /// Gets the eligible extension methods.
        /// </summary>
        /// <param name="substituteInferredTypes">
        /// Specifies whether to produce a <see cref="SpecializedMethod"/>
        /// when type arguments could be inferred from <see cref="TargetType"/>.
        /// This setting is only used for inferred types and has no effect if the type parameters are
        /// specified explicitly.
        /// </param>
        /// <remarks>
        /// The results are stored in nested lists because they are grouped by using scope.
        /// That is, for "using SomeExtensions; namespace X { using MoreExtensions; ... }",
        /// the return value will be
        /// new List {
        ///    new List { all extensions from MoreExtensions },
        ///    new List { all extensions from SomeExtensions }
        /// }
        /// </remarks>
        public IEnumerable <IEnumerable <IMethod> > GetEligibleExtensionMethods(bool substituteInferredTypes)
        {
            var result = new List <List <IMethod> >();

            foreach (var methodGroup in GetExtensionMethods())
            {
                var outputGroup = new List <IMethod>();
                foreach (var method in methodGroup)
                {
                    IType[] inferredTypes;
                    if (CSharpResolver.IsEligibleExtensionMethod(this.TargetType, method, true, out inferredTypes))
                    {
                        if (substituteInferredTypes && inferredTypes != null)
                        {
                            outputGroup.Add(method.Specialize(new TypeParameterSubstitution(null, inferredTypes)));
                        }
                        else
                        {
                            outputGroup.Add(method);
                        }
                    }
                }
                if (outputGroup.Count > 0)
                {
                    result.Add(outputGroup);
                }
            }
            return(result);
        }
Example #2
0
        public void FirstIsEligibleExtensionMethod()
        {
            string program    = @"using System; using System.Collections.Generic;
public static class XC {
	$public static TSource First<TSource>(this IEnumerable<TSource> source) {}$
}
";
            var    mrr        = Resolve <MemberResolveResult>(program);
            var    targetType = compilation.FindType(typeof(string[]));

            IType[] inferredTypes;
            bool    isEligible = CSharpResolver.IsEligibleExtensionMethod(targetType, (IMethod)mrr.Member, true, out inferredTypes);

            Assert.IsTrue(isEligible);
            Assert.AreEqual(1, inferredTypes.Length);
            Assert.AreEqual("System.String", inferredTypes[0].ReflectionName);
        }