Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NExtensionMethod"/> class.
        /// </summary>
        public NExtensionMethod(INTypeReference extendedType, NMethod extensionMethod)
        {
            ExtendedType = extendedType;

            Method                   = new NMethod();
            Method.Assembly          = extensionMethod.Assembly;
            Method.Description       = extensionMethod.Description;
            Method.DocNode           = extensionMethod.DocNode;
            Method.ElementType       = extensionMethod.ElementType;
            Method.FullName          = extensionMethod.FullName;
            Method.GenericArguments  = extensionMethod.GenericArguments;
            Method.GenericParameters = extensionMethod.GenericParameters;
            Method.HasOverrides      = extensionMethod.HasOverrides;
            Method.Id                = extensionMethod.Id;
            Method.Implements        = extensionMethod.Implements;
            Method.IsAbstract        = extensionMethod.IsAbstract;
            Method.IsObsolete        = extensionMethod.IsObsolete;
            Method.IsVirtual         = extensionMethod.IsVirtual;
            Method.IsObsolete        = extensionMethod.IsObsolete;
            Method.MsdnId            = extensionMethod.MsdnId;
            Method.Name              = extensionMethod.Name;
            Method.ObsoleteMessage   = extensionMethod.ObsoleteMessage;
            Method.PageId            = extensionMethod.PageId;
            Method.PageTitle         = extensionMethod.PageTitle;
            Method.Remarks           = extensionMethod.Remarks;
            Method.SeeAlsos          = extensionMethod.SeeAlsos;
            Method.TopicLink         = extensionMethod.TopicLink;
            Method.UnManagedApi      = extensionMethod.UnManagedApi;
            Method.UnManagedShortApi = extensionMethod.UnManagedShortApi;
            Method.Namespace         = extensionMethod.Namespace;
            Method.Visibility        = extensionMethod.Visibility;
            Method.ReturnDescription = extensionMethod.ReturnDescription;
            Method.ReturnType        = extensionMethod.ReturnType;
            Method.MemberType        = extensionMethod.MemberType;
            foreach (var group in extensionMethod.Groups)
            {
                Method.SetApiGroup(group, true);
            }

            // The extension method as member method is not static
            Method.IsStatic = false;


            Method.MemberType        = NMemberType.Extension;
            Method.IsExtensionMethod = true;
            Method.DeclaringType     = extendedType.ElementType;
            Method.ExtensionSource   = extensionMethod.DeclaringType;


            // Remove the "this parameter"
            Method.Parameters = new List <NParameter>(extensionMethod.Parameters);
            Method.Parameters.RemoveAt(0);

            // Rebuild the signature without the "this parameter"
            var signature = new StringBuilder();

            signature.Append(Method.Name);
            MonoCecilModelBuilder.BuildMethodSignatureParameters(Method, signature);
            Method.Signature = signature.ToString();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NExtensionMethod"/> class.
        /// </summary>
        public NExtensionMethod(INTypeReference extendedType, NMethod extensionMethod)
        {
            ExtendedType = extendedType;

            Method = new NMethod();
            Method.Assembly = extensionMethod.Assembly;
            Method.Description = extensionMethod.Description;
            Method.DocNode = extensionMethod.DocNode;
            Method.ElementType = extensionMethod.ElementType;
            Method.FullName = extensionMethod.FullName;
            Method.GenericArguments = extensionMethod.GenericArguments;
            Method.GenericParameters = extensionMethod.GenericParameters;
            Method.HasOverrides = extensionMethod.HasOverrides;
            Method.Id = extensionMethod.Id;
            Method.Implements = extensionMethod.Implements;
            Method.IsAbstract = extensionMethod.IsAbstract;
            Method.IsObsolete = extensionMethod.IsObsolete;
            Method.IsVirtual = extensionMethod.IsVirtual;
            Method.IsObsolete = extensionMethod.IsObsolete;
            Method.MsdnId = extensionMethod.MsdnId;
            Method.Name = extensionMethod.Name;
            Method.ObsoleteMessage = extensionMethod.ObsoleteMessage;
            Method.PageId = extensionMethod.PageId;
            Method.PageTitle = extensionMethod.PageTitle;
            Method.Remarks = extensionMethod.Remarks;
            Method.SeeAlsos = extensionMethod.SeeAlsos;
            Method.TopicLink = extensionMethod.TopicLink;
            Method.UnManagedApi = extensionMethod.UnManagedApi;
            Method.UnManagedShortApi = extensionMethod.UnManagedShortApi;
            Method.Namespace = extensionMethod.Namespace;
            Method.Visibility = extensionMethod.Visibility;
            Method.ReturnDescription = extensionMethod.ReturnDescription;
            Method.ReturnType = extensionMethod.ReturnType;
            Method.MemberType = extensionMethod.MemberType;
            foreach (var group in extensionMethod.Groups)
                Method.SetApiGroup(group, true);

            // The extension method as member method is not static
            Method.IsStatic = false;


            Method.MemberType = NMemberType.Extension;
            Method.IsExtensionMethod = true;
            Method.DeclaringType = extendedType.ElementType;
            Method.ExtensionSource = extensionMethod.DeclaringType;

            
            // Remove the "this parameter"
            Method.Parameters = new List<NParameter>(extensionMethod.Parameters);
            Method.Parameters.RemoveAt(0);

            // Rebuild the signature without the "this parameter"
            var signature = new StringBuilder();
            signature.Append(Method.Name);
            MonoCecilModelBuilder.BuildMethodSignatureParameters(Method, signature);
            Method.Signature = signature.ToString();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Resolve a document Id (i.e. "T:System.Object") to an URL.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="linkName">Name of the link.</param>
        /// <param name="forceLocal">if set to <c>true</c> [force local].</param>
        /// <param name="attributes">The attributes.</param>
        /// <param name="localReference">The local reference.</param>
        /// <param name="useSelf"></param>
        /// <returns></returns>
        public string ToUrl(string id, string linkName = null, bool forceLocal = false, string attributes = null, IModelReference localReference = null, bool useSelf = true)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                return("#");
            }

            id = id.Trim();
            if (id.StartsWith("!:"))
            {
                id = id.Substring(2);
            }

            if (id.Length == 0 || id == "#")
            {
                return(linkName ?? id);
            }

            var linkDescriptor = new LinkDescriptor {
                Type = LinkType.None, Index = -1
            };
            var             typeReference      = localReference as INTypeReference;
            INTypeReference genericInstance    = null;
            bool            isGenericInstance  = typeReference != null && typeReference.IsGenericInstance;
            bool            isGenericParameter = typeReference != null && typeReference.IsGenericParameter;

            if (isGenericInstance)
            {
                var elementType = typeReference.ElementType;
                id = elementType.Id;
                genericInstance = typeReference;
                linkName        = elementType.Name.Substring(0, elementType.Name.IndexOf('<'));
            }

            linkDescriptor.LocalReference = FindLocalReference(id);

            if (isGenericParameter)
            {
                linkDescriptor.Name = typeReference.Name;
            }
            else if (linkDescriptor.LocalReference != null)
            {
                // For local references, use short name
                var method = linkDescriptor.LocalReference as NMethod;
                if (method != null)
                {
                    linkDescriptor.Name = method.Signature;
                }
                else
                {
                    linkDescriptor.Name = linkDescriptor.LocalReference.Name;
                }

                linkDescriptor.PageId = linkDescriptor.LocalReference.PageId;
                linkDescriptor.Type   = LinkType.Local;
                linkDescriptor.Index  = linkDescriptor.LocalReference.Index;
                if (!forceLocal && CurrentContext != null && linkDescriptor.LocalReference is NMember)
                {
                    var declaringType = ((NMember)linkDescriptor.LocalReference).DeclaringType;
                    // If link is self referencing the current context, then use a self link
                    if ((id == CurrentContext.Id || (declaringType != null && declaringType.Id == CurrentContext.Id && (!id.StartsWith("T:") && !declaringType.Id.StartsWith("T:")))) && useSelf)
                    {
                        linkDescriptor.Type = LinkType.Self;
                    }
                }
            }
            else
            {
                linkDescriptor.Type = LinkType.External;
                if (id.StartsWith("http:") || id.StartsWith("https:"))
                {
                    linkDescriptor.Location = id;
                }
                else
                {
                    // Try to resolve to MSDN
                    linkDescriptor.Location = _msnRegistry.FindUrl(id);
                    var reference = TextReferenceUtility.CreateReference(id);
                    if (linkDescriptor.Location != null)
                    {
                        linkDescriptor.Name = reference.FullName;
                        // Open MSDN documentation to a new window
                        attributes = (attributes ?? "") + " target='_blank'";
                    }
                    else
                    {
                        linkDescriptor.Name = reference.Name;
                    }
                }
            }

            if (LinkResolver == null)
            {
                Logger.Warning("Model.LinkResolver must be set");
                return(id);
            }

            if (linkName != null)
            {
                linkDescriptor.Name = linkName;
            }

            linkDescriptor.Id         = id;
            linkDescriptor.Attributes = attributes;

            var urlBuilder = new StringBuilder();

            urlBuilder.Append(LinkResolver(linkDescriptor));

            // Handle URL for generic instance
            if (genericInstance != null)
            {
                urlBuilder.Append("&lt;");
                for (int i = 0; i < genericInstance.GenericArguments.Count; i++)
                {
                    if (i > 0)
                    {
                        urlBuilder.Append(", ");
                    }
                    var genericArgument = genericInstance.GenericArguments[i];
                    // Recursive call here
                    urlBuilder.Append(ToUrl(genericArgument));
                }
                urlBuilder.Append("&gt;");
            }
            return(urlBuilder.ToString());
        }