public static MemberReference GetMember(TypeDefinition type, DocumentationMember member)
        {
            string membertype = member.MemberType;

            string returntype = member.ReturnType;

            string docName = member.MemberName;

            string[] docTypeParams = GetTypeParameters(docName, member.TypeParameters);

            // If we're using 'magic types', then we might get false positives ... in those cases, we keep searching
            MemberReference likelyCandidate = null;

            // Loop through all members in this type with the same name
            var reflectedMembers = GetReflectionMembers(type, docName, membertype).ToArray();

            foreach (MemberReference mi in reflectedMembers)
            {
                bool matchedMagicType = false;
                if (mi is TypeDefinition)
                {
                    continue;
                }
                if (MDocUpdater.GetMemberType(mi) != membertype)
                {
                    continue;
                }

                if (MDocUpdater.IsPrivate(mi))
                {
                    continue;
                }

                IList <ParameterDefinition> pis = null;
                string[] typeParams             = null;
                if (mi is MethodDefinition)
                {
                    MethodDefinition mb = (MethodDefinition)mi;
                    pis = mb.Parameters;
                    if (mb.IsGenericMethod())
                    {
                        IList <GenericParameter> args = mb.GenericParameters;
                        typeParams = args.Select(p => p.Name).ToArray();
                    }
                }
                else if (mi is PropertyDefinition)
                {
                    pis = ((PropertyDefinition)mi).Parameters;
                }

                // check type parameters
                int methodTcount     = member.TypeParameters == null ? 0 : member.TypeParameters.Count;
                int reflectionTcount = typeParams == null ? 0 : typeParams.Length;
                if (methodTcount != reflectionTcount)
                {
                    continue;
                }

                // check member parameters
                int mcount = member.Parameters == null ? 0 : member.Parameters.Count;
                int pcount = pis == null ? 0 : pis.Count;
                if (mcount != pcount)
                {
                    continue;
                }

                MethodDefinition mDef = mi as MethodDefinition;
                if (mDef != null && !mDef.IsConstructor && (mDef.Name.StartsWith("op_Explicit", StringComparison.Ordinal) || mDef.Name.StartsWith("op_Implicit", StringComparison.Ordinal)))
                {
                    // Casting operators can overload based on return type.
                    string rtype = GetReplacedString(
                        MDocUpdater.GetDocTypeFullName(((MethodDefinition)mi).ReturnType),
                        typeParams, docTypeParams);
                    string originalRType = rtype;
                    if (MDocUpdater.SwitchingToMagicTypes)
                    {
                        rtype = NativeTypeManager.ConvertFromNativeType(rtype);
                    }
                    if ((returntype != rtype && originalRType == rtype) ||
                        (MDocUpdater.SwitchingToMagicTypes && returntype != originalRType && returntype != rtype && originalRType != rtype))
                    {
                        continue;
                    }

                    if (originalRType != rtype)
                    {
                        matchedMagicType = true;
                    }
                }

                if (pcount == 0)
                {
                    return(mi);
                }
                bool isExtensionMethod = DocUtils.IsExtensionMethod(mDef);
                bool good = true;
                for (int i = 0; i < pis.Count; i++)
                {
                    bool isRefType = pis[i].ParameterType is ByReferenceType;

                    if (i == 0 && !isRefType && isExtensionMethod)
                    {
                        isRefType = true; // this will be the case for generic parameter types
                    }
                    string paramType = GetReplacedString(
                        MDocUpdater.GetDocParameterType(pis[i].ParameterType),
                        typeParams, docTypeParams);

                    // if magictypes, replace paramType to "classic value" ... so the comparison works
                    string originalParamType = paramType;
                    if (MDocUpdater.SwitchingToMagicTypes)
                    {
                        paramType = NativeTypeManager.ConvertFromNativeType(paramType);
                    }

                    string xmlMemberType = member.Parameters[i];

                    // TODO: take into account extension method reftype
                    bool xmlIsRefType  = xmlMemberType.Contains('&');
                    bool refTypesMatch = isRefType == xmlIsRefType;

                    if (!refTypesMatch)
                    {
                        good = false;
                        break;
                    }

                    xmlMemberType = xmlIsRefType ? xmlMemberType.Substring(0, xmlMemberType.Length - 1) : xmlMemberType;

                    if ((!paramType.Equals(xmlMemberType) && paramType.Equals(originalParamType)) ||
                        (MDocUpdater.SwitchingToMagicTypes && !originalParamType.Equals(xmlMemberType) && !paramType.Equals(xmlMemberType) && !paramType.Equals(originalParamType)))
                    {
                        // did not match ... if we're dropping the namespace, and the paramType has the dropped
                        // namespace, we should see if it matches when added
                        bool stillDoesntMatch = true;
                        if (MDocUpdater.HasDroppedNamespace(type) && paramType.StartsWith(MDocUpdater.droppedNamespace))
                        {
                            string withDroppedNs = string.Format("{0}.{1}", MDocUpdater.droppedNamespace, xmlMemberType);

                            stillDoesntMatch = withDroppedNs != paramType;
                        }

                        if (stillDoesntMatch)
                        {
                            good = false;
                            break;
                        }
                    }

                    if (originalParamType != paramType)
                    {
                        matchedMagicType = true;
                    }
                }
                if (!good)
                {
                    continue;
                }

                if (MDocUpdater.SwitchingToMagicTypes && likelyCandidate == null && matchedMagicType)
                {
                    // we matched this on a magic type conversion ... let's keep going to see if there's another one we should look at that matches more closely
                    likelyCandidate = mi;
                    continue;
                }

                return(mi);
            }

            return(likelyCandidate);
        }
示例#2
0
        private IEnumerable <DocsNodeInfo> GetMembers(XmlDocument basefile, TypeDefinition type, FrameworkTypeEntry typeEntry)
        {
            while (ecmadocs.Name != "Members" && ecmadocs.Read())
            {
                // do nothing
            }
            if (ecmadocs.IsEmptyElement)
            {
                yield break;
            }

            int  membersDepth = ecmadocs.Depth;
            bool go           = true;

            while (go && ecmadocs.Read())
            {
                switch (ecmadocs.Name)
                {
                case "Member":
                {
                    if (membersDepth != ecmadocs.Depth - 1 || ecmadocs.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }
                    DocumentationMember dm = new DocumentationMember(ecmadocs);

                    string          xp        = MDocUpdater.GetXPathForMember(dm);
                    XmlElement      oldmember = (XmlElement)basefile.SelectSingleNode(xp);
                    MemberReference m;
                    if (oldmember == null)
                    {
                        m = GetMember(type, dm);
                        if (m == null)
                        {
                            app.Warning("Could not import ECMA docs for `{0}'s `{1}': Member not found.",
                                        type.FullName, dm.MemberSignatures["C#"]);
                            // SelectSingleNode (ecmaDocsMember, "MemberSignature[@Language=\"C#\"]/@Value").Value);
                            continue;
                        }
                        // oldmember lookup may have failed due to type parameter renames.
                        // Try again.
                        oldmember = (XmlElement)basefile.SelectSingleNode(MDocUpdater.GetXPathForMember(m));
                        if (oldmember == null)
                        {
                            XmlElement members = MDocUpdater.WriteElement(basefile.DocumentElement, "Members");
                            oldmember = basefile.CreateElement("Member");
                            oldmember.SetAttribute("MemberName", dm.MemberName);
                            members.AppendChild(oldmember);
                            foreach (string key in MDocUpdater.Sort(dm.MemberSignatures.Keys))
                            {
                                XmlElement ms = basefile.CreateElement("MemberSignature");
                                ms.SetAttribute("Language", key);
                                ms.SetAttribute("Value", (string)dm.MemberSignatures[key]);
                                oldmember.AppendChild(ms);
                            }
                            oldmember.SetAttribute("__monodocer-seen__", "true");
                            Console.WriteLine("Member Added: {0}", oldmember.SelectSingleNode("MemberSignature[@Language='C#']/@Value").InnerText);
                            app.additions++;
                        }
                    }
                    else
                    {
                        m = GetMember(type, new DocumentationMember(oldmember, typeEntry));
                        if (m == null)
                        {
                            app.Warning("Could not import ECMA docs for `{0}'s `{1}': Member not found.",
                                        type.FullName, dm.MemberSignatures["C#"]);
                            continue;
                        }
                        oldmember.SetAttribute("__monodocer-seen__", "true");
                    }
                    DocsNodeInfo node = new DocsNodeInfo(oldmember, m);
                    if (ecmadocs.Name != "Docs")
                    {
                        throw new InvalidOperationException("Found " + ecmadocs.Name + "; expected <Docs/>!");
                    }
                    yield return(node);

                    break;
                }

                case "Members":
                    if (membersDepth == ecmadocs.Depth && ecmadocs.NodeType == XmlNodeType.EndElement)
                    {
                        go = false;
                    }
                    break;
                }
            }
        }