C# 如何在intellisense中显示自定义ToString()格式?
我想在intellisense中显示自定义ToString()格式,如下面的C# 如何在intellisense中显示自定义ToString()格式?,c#,.net,visual-studio,visual-studio-2019,intellisense,C#,.net,Visual Studio,Visual Studio 2019,Intellisense,我想在intellisense中显示自定义ToString()格式,如下面的DateTime.ToString() 下面是示例代码,当有人键入myObject.ToString(“”)时,我想在intellisense中显示“a”或“b” 你可以用这个 例如,对于您的ToString()内容: public class MyClass : IFormattable { /// <summary>Converts this to a formatted string.<
DateTime.ToString()
下面是示例代码,当有人键入myObject.ToString(“”
)时,我想在intellisense中显示“a”或“b”
你可以用这个
例如,对于您的ToString()
内容:
public class MyClass : IFormattable
{
/// <summary>Converts this to a formatted string.</summary>
/// <param name="format">
/// A format string. This may have the following values:
/// <list type="table">
/// <listheader>
/// <term>Format strings</term>
/// </listheader>
/// <item>
/// <term>"a"</term>
/// <description>Format using "a"</description>
/// </item>
/// <item>
/// <term>"b"</term>
/// <description>Format using "b"</description>
/// </item>
/// </list>
/// </param>
/// <param name="formatProvider">A format provider.</param>
/// <returns>The formatted string.</returns>
public string ToString(string format, IFormatProvider formatProvider)
{
switch (format)
{
case "a":
return "A";
case "b":
return "B";
default:
return "A";
}
}
}
公共类MyClass:IFormattable
{
///将其转换为格式化字符串。
///
///格式字符串。该字符串可能具有以下值:
///
///
///格式化字符串
///
///
///“a”
///使用“a”设置格式
///
///
///“b”
///格式使用“b”
///
///
///
///格式提供程序。
///格式化的字符串。
公共字符串到字符串(字符串格式,IFormatProvider formatProvider)
{
开关(格式)
{
案例“a”:
返回“A”;
案例“b”:
返回“B”;
违约:
返回“A”;
}
}
}
@John谢谢,但我不认为我的问题是通过DebuggerDisplay
解决的。您好,这很有效,但它不会像截图中那样生成intellisense。我想知道是否有可能出现类似于所讨论的屏幕截图的内容。@据我所知,内置的intellisense是特殊情况,因此XML文档注释是最接近您可能得到的。@imlokesh,如果您想扩展intellisense for tostring(),您必须创建一个vsix项目,这是VSSDK项目的工作,有点复杂。所以马修是一个更接近和更容易实现的方法。“如果你的回答能帮助你处理这个问题,你可以考虑接受它。”只是等着看别人是否还有什么要补充的。
public class MyClass : IFormattable
{
/// <summary>Converts this to a formatted string.</summary>
/// <param name="format">
/// A format string. This may have the following values:
/// <list type="table">
/// <listheader>
/// <term>Format strings</term>
/// </listheader>
/// <item>
/// <term>"a"</term>
/// <description>Format using "a"</description>
/// </item>
/// <item>
/// <term>"b"</term>
/// <description>Format using "b"</description>
/// </item>
/// </list>
/// </param>
/// <param name="formatProvider">A format provider.</param>
/// <returns>The formatted string.</returns>
public string ToString(string format, IFormatProvider formatProvider)
{
switch (format)
{
case "a":
return "A";
case "b":
return "B";
default:
return "A";
}
}
}