简体   繁体   English

在 XML 文档中,如何在同一命名空间中引用另一个具有相同名称的 class?

[英]In XML documenttion, how do I reference another class that has the same name in the same namespace?

I have several C# packages that are closely related to each other.我有几个彼此密切相关的 C# 包。 Each one of these has its own IServiceCollectionsExtensions static class living in the Microsoft.Extensions.DependencyInjection namespace.其中每一个都有自己的IServiceCollectionsExtensions static class 位于Microsoft.Extensions.DependencyInjection命名空间中。 What I want to do is in the XML comments of one these methods, refer to a method in the other class in the other assembly.我想做的是在其中一个方法的 XML 注释中,在另一个程序集中引用另一个 class 中的方法。 Example:例子:

// assembly Foo
namespace Microsoft.Extensions.DependencyInjection
{
    public static class IServiceCollectionExtensions
    {
        /// <summary>This is some info</summary>
        public static IServiceCollection AddFoo(this IServiceCollection services)
        {
            // register some types in the DI container
            return services;
        }
    }
}

// assembly Foo.Bar, which has a dependency on Foo
namespace Microsoft.Extensions.DependencyInjection
{
    public static class IServiceCollectionExtensions
    {
        /// <summary>
        /// You don't need to call <see cref="IServiceCollectionsExtensions.AddFoo(IServiceCollection)" /> -- we'll do it for you
        /// </summary>
        public static IServiceCollection AddFooBar(this IServiceCollection services)
        {
            services.AddFoo();
            // register some other types in the DI container
            return services;
        }
    }
}

The problem is in the Foo.Bar project, Visual Studio complains that "XML has cref attribute 'AddFoo(IServiceCollection)' that could not be resolved" (CS1574).问题出在 Foo.Bar 项目中,Visual Studio 抱怨“XML 具有无法解析的 cref 属性‘AddFoo(IServiceCollection)’”(CS1574)。

Note that this is a contrived example.请注意,这是一个人为的例子。 I'm perfectly aware that you can register the same services multiple times in most cases.我非常清楚在大多数情况下您可以多次注册相同的服务。

How can I accomplish this?我怎样才能做到这一点? Thanks in advance.提前致谢。

The best answer might be "don't do that", and just name your extension method classes different names.最好的答案可能是“不要那样做”,只需将扩展方法类命名为不同的名称即可。 If they're extension methods after all, the name of the class usually doesn't matter.如果它们毕竟是扩展方法,那么 class 的名称通常并不重要。 (Defining stuff inside the "Microsoft" namespace is also a bit confusing here, since well, you're not shipping this in a Microsoft assembly, I'm guessing!) (在“Microsoft”命名空间内定义内容在这里也有点令人困惑,因为好吧,我猜你不会在 Microsoft 程序集中发布它!)

If you really want to do that, in a cref you can start your name with T: to reference a type explicitly or M: to reference a method explicitly, but at that point you opt out of all validation or IDE features since that's the backdoor.如果你真的想这样做,在 cref 中你可以以 T: 开头来显式引用类型或 M: 来显式引用方法,但此时你选择退出所有验证或 IDE 功能,因为那是后门.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM