简体   繁体   English

c#xml注释,cref不能引用基类中定义的方法

[英]c# xml comment, cref can't reference method defined in base class

In my codebase I have a method defined in a base class, the base class is inherited, but the method is not yet overridden.在我的代码库中,我在基类中定义了一个方法,该基类是继承的,但该方法尚未被覆盖。 This method will very likely be overridden in the future to add to the base implementation.将来很可能会覆盖此方法以添加到基本实现中。

My setup looks a bit like this:我的设置看起来有点像这样:

public abstract class BaseFoo
{
    public virtual void Bar()
    {
        //default implementation
    }
}

public class RealFoo : BaseFoo
{
    //extra code, does *NOT YET* override Bar but might in the future
}

public class DependentClass
{
    /// <summary>
    /// Uses <see cref="RealFoo.Bar"/> to do some magic
    /// </summary>
    public void SomeMethod()
    {

    }
}

Since it's very likely that Bar will be overridden in the future, I would like to future proof my xmldoc and reference RealFoo.Bar instead of BaseFoo.Bar .由于将来很可能 Bar 将被覆盖,我想将来证明我的 xmldoc 并引用RealFoo.Bar而不是BaseFoo.Bar

When I call RealFoo.Bar() in my code, no errors occur.当我在代码中调用RealFoo.Bar()时,没有发生错误。 When I do so in a cref attribute I get the following warning:当我在 cref 属性中这样做时,我收到以下警告:

Warning CS1574 XML comment has cref attribute 'Bar' that could not be resolved.警告 CS1574 XML 注释具有无法解析的 cref 属性“Bar”。

Am I doing something wrong here or is this just a limitation of cref?我在这里做错了什么还是这只是 cref 的限制?

I'm using visual studio 2017, targeting netstandard2.0 and net452, and I have XML documentation enabled in my csproj.我正在使用 Visual Studio 2017,针对 netstandard2.0 和 net452,并且在我的 csproj 中启用了 XML 文档。

You can suppress this warning, but the XmlDoc output will mark it with an error.您可以取消此警告,但 XmlDoc 输出会将其标记为错误。

<member name="M:MyApplication.DependentClass.SomeMethod">
    <summary>
        Uses <see cref="!:RealFoo.Bar"/> to do some magic
        <!--            ^- indicates an error -->
    </summary>
</member>

Alternatively, if you're pretty sure that you're going to override the method in the future, and you are intending to generate documentation with the XML output, I would just implement it in RealFoo as public override void Bar() => base.Bar();或者,如果您非常确定将来要覆盖该方法,并且打算使用 XML 输出生成文档,我将在RealFoo实现它作为public override void Bar() => base.Bar(); until you want a new implementation.直到你想要一个新的实现。

In this case I just statically write ClassName.Member which does the trick.在这种情况下,我只是静态地编写 ClassName.Member 来解决问题。

/// <summary>
/// Uses <see cref="BaseFoo.Bar"/> to do some magic
/// </summary>
public void SomeMethod()
{

}

It looks like .NET Xml-Doc does not know about inheritance看起来 .NET Xml-Doc 不知道继承

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

相关问题 如何为复杂表达式添加 c# xml cref 注释? - How to add c# xml cref comment for a complex expression? 使用C#的XML注释cref属性和params语法 - Using C#'s XML comment cref attribute with params syntax 我可以调用基于 class 的 static 方法,并参考实现该基础 ZA2F2ED4F8EBC2CBB4C21 的 class 吗? - Can I call a static method that lives on a base class with a reference the the class implementing that base class (in C#)? 为什么我不能从派生类的XML文档注释标记中引用基类中的可访问成员? - Why can't I reference the accessible members in a base class from within XML documentation comment tags in a derived class? 如何从C#XML注释中引用构造函数? - How can I reference a constructor from C# XML comment? 我可以用吗<inheritdoc cref>引用另一个变量的 XML 摘要?</inheritdoc> - Can I use <inheritdoc cref> to reference the XML summary of another variable? c#xml代码注释用于文件引用 - c# xml code comment for file reference 如何使一个cref方法重载一个 <seealso> C#中的标签? - How to make a cref to method overloads in a <seealso> tag in C#? <see cref>字节[]的XML注释没有错误 - <see cref> XML comment for byte[] without errors 有没有办法在 C# 中获取对基类的引用? - Is there a way to get a reference to the base class in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM