Home > Net >  In XML documenttion, how do I reference another class that has the same name in the same namespace?
In XML documenttion, how do I reference another class that has the same name in the same namespace?

Time:04-12

I have several C# packages that are closely related to each other. Each one of these has its own IServiceCollectionsExtensions static class living in the Microsoft.Extensions.DependencyInjection namespace. 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. 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).

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.

CodePudding user response:

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. (Defining stuff inside the "Microsoft" namespace is also a bit confusing here, since well, you're not shipping this in a Microsfot assembly, I'm guessing!)

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.

  • Related