【问题标题】:display constants for custom object, similar to MessageBoxButtons自定义对象的显示常量,类似于 MessageBoxButtons
【发布时间】:2013-05-17 04:38:59
【问题描述】:

好的,所以我有一个奇怪的问题 - 我不确定我的措辞是否正确,这可能是我在搜索中没有找到任何相关信息的原因。

我有一个类,它定义了一个主机对象,该对象代表一台计算机,其中记录了有关该计算机的各种信息。

public sealed class Host
{
    public Host(string sName, IPAddress sAddress, string sType, string osName, bool sFirewall)
    {
        Name = sName;
        Address = sAddress;
        Type = sType;
        FirewallActive = sFirewall;
        OperatingSystem = osName;
    }

    public Host()
    {
        Name = "New Host";
        Address = IPAddress.Parse("127.0.0.1");
        Type = HostType.Desktop;
        OperatingSystem = HostOS.Win7;
        FirewallActive = true;
    }

    /// <summary>
    /// The name of the host
    /// </summary>
    public string Name { get; private set; }

    /// <summary>
    /// The ip address of the host
    /// </summary>
    public IPAddress Address { get; private set; }

    /// <summary>
    /// The type of the host
    /// </summary>
    public string Type { get; private set; }

    /// <summary>
    /// The operating system the system uses
    /// </summary>
    public string OperatingSystem { get; private set; }

    /// <summary>
    /// Whether the system has a firewall enabled
    /// </summary>
    public bool FirewallActive { get; private set; }
}

然后我有几个对象,其中几个设置具有常量值。

public sealed class HostType
{
    public static string Desktop
    {
        get { return "Desktop"; }
    }
}

public sealed class HostOS
{
    public static string Win7
    {
        get { return "Windows 7"; }
    }
}

当我创建一个新的 Host 对象时,我希望 Intellisense 在构建新的 Hosts([parameters]) 对象时自动提示输入“HostOS”变量,类似于使用 MessageBox 时的方式.Show(...) 当您到达参数列表的那部分时,它会自动建议各种 MessageBoxButtons 选项的列表。

我不想修改列表,我只想让 Intellisense 向我显示一个选项列表,这些选项是各种 HostOS 常量字符串。

【问题讨论】:

    标签: c# visual-studio-2012 intellisense


    【解决方案1】:

    您应该将其定义为枚举而不是类。

    例如:

    public enum HostType
    {
      Desktop,
      Server,
      Laptop
    }
    

    在类主机中,您必须将属性 Type 定义为 HostType

    public HostType Type { get; private set }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 2015-01-01
      • 2011-05-05
      • 1970-01-01
      相关资源
      最近更新 更多