c# - 词典 - 添加注释以驱动智能感知

标签 c# dictionary intellisense

有没有一种方法可以添加注释来记录 Dictionary 或 ConcurrentDictionary 以了解键/值的含义?

例如:

Dictionary<guid, string> _users;

这个例子有一个用户字典。 guid 是 UserId,string 是用户名,但除了“知道”之外很难说。

有没有一种方法可以添加文档,以便在您添加项目时,intellisense 会告诉开发人员有关键和值的注释?

我知道我可以添加 <summary>在其上方评论并将该注释放在对象本身中,但在添加、删除等时正在寻找。

最佳答案

最近在 GOOS 书中我发现了在自己的类中打包常见类型(例如集合)的有趣想法:

Try to use the language of the problem you are working on, rather than the language of .Net constructs. It reduces conceptual gap between domain and code. Also try to limit passing around types with generics. This is a form of duplication. It's a hint that there is domain concept that should be extracted to type.

坦率地说,我在打包常见的泛型集合方面并没有那么极端,但即使给一个类型自己的名字也会让它匹配起来更容易阅读和理解:

public class UserNameDictionary : Dictionary<int, string>
{
}

很简单。现在什么更好读:

Dictionary<int, string> users = new Dictionary<int, string>();
UserNameDictionary users = new UserNameDictionary();

您还可以快速向您的类(class)添加评论:

/// <summary>
/// Represents a dictionary of user names accessed by ids.
/// </summary>

这不会像Add(int, string)这样的方法添加注释,但是当其他人使用这个类时,他们会在UserNameDictionary的上下文中思考。 ,不是抽象的Dictionary<int, string>上下文。

如果你想让你的类更得心应手,你可以隐藏基类方法:

public new void Add(int userId, string userName)
{
    base.Add(userId, userName);
}

对于更复杂的用例,我会使用将工作委托(delegate)给内部字典的自定义类。

关于c# - 词典 - 添加注释以驱动智能感知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10421433/

相关文章:

c# - WOPI 主机实现,试图在 iframe asp.net mvc 中呈现文档

c# - 带输入字段的 FolderBrowserDialog

python - 循环中嵌套字典

visual-studio-2010 - 如何让 VS 2010 识别某些 CUDA 函数

android - 如何在 Flutter 项目中让 Intellij Idea 的 Intellisense 在 C++ 和 Kotlin 上工作?

c# - 将服务与计时器同步

python - 将 dict 中的键添加到现有 Pandas 数据框中的列标题

arrays - 以列表为值的 Bash 关联数组

react-native - VSCode 智能感知不适用于 ES6 导入?

c# - 在 C# 中保存图元文件