예제 #1
0
        /// <summary>
        /// Finds the member this <see cref="CRefPath"/> refers to in the provided type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>The found member ref.</returns>
        /// <exception cref="ArgumentNullException">When <paramref name="type"/> is null.</exception>
        public ReflectedMember FindIn(TypeDef type)
        {
            // TODO: Move to the TypeDef class
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (this.PathType == CRefTypes.Namespace || this.PathType == CRefTypes.Type || this.PathType == CRefTypes.Error)
            {
                throw new InvalidOperationException(string.Format("Can not find member in a type when the path type is '{0}'", this.PathType.ToString()));
            }

            ReflectedMember        member       = null;
            List <ReflectedMember> foundMembers = new List <ReflectedMember>();

            // find all potential members
            switch (this.PathType)
            {
            case CRefTypes.Event:
                foundMembers.AddRange(type.Events.FindAll(e => string.Compare(e.Name, this.ElementName, true) == 0).ToArray());
                break;

            case CRefTypes.Field:
                foundMembers.AddRange(type.Fields.FindAll(e => string.Compare(e.Name, this.ElementName, true) == 0).ToArray());
                break;

            case CRefTypes.Method:
                string elementName   = this.ElementName.Replace('#', '.');
                int    genParameters = 0;
                if (elementName.Contains('`'))
                {
                    genParameters = int.Parse(elementName.Substring(elementName.Length - 1, 1));
                    elementName   = elementName.Substring(0, elementName.IndexOf('`'));
                }
                MethodDef[] foundMethods = type.Methods.FindAll(e => string.Compare(e.Name, elementName, true) == 0).ToArray();
                if (foundMethods.Length > 1 && genParameters > 0)
                {
                    for (int i = 0; i < foundMethods.Length; i++)
                    {
                        if (foundMethods[i].GenericTypes != null && foundMethods[i].GenericTypes.Count == genParameters)
                        {
                            foundMembers.Add(foundMethods[i]);
                        }
                    }
                }
                else
                {
                    foundMembers.AddRange(foundMethods);
                }
                break;

            case CRefTypes.Property:
                foundMembers.AddRange(type.Properties.FindAll(e => string.Compare(e.Name, this.ElementName, true) == 0).ToArray());
                break;
            }

            if (foundMembers.Count == 1)
            {
                member = foundMembers[0];
            }
            else if (foundMembers.Count > 1)
            {
                // the elements will differ by the parameters, this is slow!
                foreach (ReflectedMember current in foundMembers)
                {
                    string found = CRefPath.Create(current).ToString();
                    if (string.Compare(found, this.ToString(), true) == 0)
                    {
                        member = current;
                        break;
                    }
                }
            }

            return(member);
        }