/// <summary> /// Determines whether an extension method is hidden by a member that's already defined on the type being extended. /// The extension method is hidden if it has the same name, template params, and parameters as a defined method. /// </summary> /// <param name="extensionMethod">The extension method to compare.</param> /// <param name="members">A dictionary of the members defined on the type being extended.</param> /// <returns></returns> private bool IsExtensionMethodHidden(Method extensionMethod, MemberDictionary members) { if (!members.MemberNames.Contains(extensionMethod.Name.Name)) { return(false); } // get a list of members with the same name as the extension method List <Member> membersList = members[extensionMethod.Name.Name]; foreach (Member member in membersList) { // the hiding member must be a method if (member.NodeType != NodeType.Method) { continue; } Method method = (Method)member; // do the generic template parameters of both methods match? if (!method.TemplateParametersMatch(extensionMethod.TemplateParameters)) { continue; } // do both methods have the same number of parameters? // (not counting the extension method's first param, which identifies the extended type) if (method.Parameters.Count != (extensionMethod.Parameters.Count - 1)) { continue; } // do the parameter types of both methods match? if (DoParameterTypesMatch(extensionMethod.Parameters, method.Parameters)) { return(true); } } return(false); }
/// <summary> /// Determines whether an extension method is hidden by a member that's already defined on the type being /// extended. The extension method is hidden if it has the same name, template parameters, and /// parameters as a defined method. /// </summary> /// <param name="extensionMethod">The extension method to compare</param> /// <param name="members">A dictionary of the members defined on the type being extended</param> /// <returns>True if hidden, false if not</returns> private static bool IsExtensionMethodHidden(Method extensionMethod, MemberDictionary members) { if (!members.MemberNames.Contains(extensionMethod.Name.Name)) { return(false); } // Get a list of members with the same name as the extension method foreach (Member member in members[extensionMethod.Name.Name]) { // The hiding member must be a method if (member.NodeType != NodeType.Method) { continue; } Method method = (Method)member; // Do the generic template parameters of both methods match? if (!method.TemplateParametersMatch(extensionMethod.TemplateParameters)) { continue; } // !EFW - Don't exclude the first parameter, it does need to be included. // Do both methods have the same number of parameters? if (method.Parameters.Count != extensionMethod.Parameters.Count) { continue; } // Do the parameter types of both methods match? if (DoParameterTypesMatch(extensionMethod.Parameters, method.Parameters)) { return(true); } } return(false); }
private void AddExtensionMethods(XmlWriter writer, Object info) { MemberDictionary members = info as MemberDictionary; if (members == null) { return; } TypeNode type = members.Type; InterfaceList contracts = type.Interfaces; foreach (Interface contract in contracts) { List <Method> extensionMethods = null; if (index.TryGetValue(contract, out extensionMethods)) { foreach (Method extensionMethod in extensionMethods) { if (!IsExtensionMethodHidden(extensionMethod, members)) { AddExtensionMethod(writer, type, extensionMethod, null); } } } if (contract.IsGeneric && (contract.TemplateArguments != null) && (contract.TemplateArguments.Count > 0)) { Interface templateContract = (Interface)ReflectionUtilities.GetTemplateType(contract); TypeNode specialization = contract.TemplateArguments[0]; if (index.TryGetValue(templateContract, out extensionMethods)) { foreach (Method extensionMethod in extensionMethods) { if (IsValidTemplateArgument(specialization, extensionMethod.TemplateParameters[0])) { if (!IsExtensionMethodHidden(extensionMethod, members)) { AddExtensionMethod(writer, type, extensionMethod, specialization); } } } } } } TypeNode comparisonType = type; while (comparisonType != null) { List <Method> extensionMethods = null; if (index.TryGetValue(comparisonType, out extensionMethods)) { foreach (Method extensionMethod in extensionMethods) { if (!IsExtensionMethodHidden(extensionMethod, members)) { AddExtensionMethod(writer, type, extensionMethod, null); } } } if (comparisonType.IsGeneric && (comparisonType.TemplateArguments != null) && (comparisonType.TemplateArguments.Count > 0)) { TypeNode templateType = ReflectionUtilities.GetTemplateType(comparisonType); TypeNode specialization = comparisonType.TemplateArguments[0]; if (index.TryGetValue(templateType, out extensionMethods)) { foreach (Method extensionMethod in extensionMethods) { if (IsValidTemplateArgument(specialization, extensionMethod.TemplateParameters[0])) { if (!IsExtensionMethodHidden(extensionMethod, members)) { AddExtensionMethod(writer, type, extensionMethod, specialization); } } } } } comparisonType = comparisonType.BaseType; } }