我想要做的是提供一些公共静态字段,这些字段将用作接口实现的实例,并且当接口是方法参数时,智能感知器会选择它们。
这个想法是让它看起来像开发人员的枚举。我引用Color,因为基本上这是我想要的行为,我只是不知道如何复制它。
修改
我不知道发生了什么事。它似乎现在正在运作。感谢您在一个愚蠢的问题上提供快速帮助。
答案 0 :(得分:3)
Color结构只有一堆返回预期对象的public static properties。样品:
public struct Color
{
public Color(int r, int g, int b)
{ /* Init */ }
public static Color Black
{
get { return new Color( 0, 0, 0 ); }
}
}
澄清我的答案。您只需要在自己的代码中复制此模式以实现相同的效果。如果你有很多需要创建的已存在的else-ware值,我建议你查看Visual Studio中内置的T4代码。只是不要添加太多。这可能会使开发人员感到困惑并降低IDE的速度。
答案 1 :(得分:2)
在提出问题时,您似乎回答了自己的问题 - 使用公共静态字段:
public class MyClass
{
public const string Value1 = "something one";
public static readonly MyType Value2 = new MyType();
public const int Value3 = 3;
}
public class MyOtherClass
{
public MyOtherClass()
{
string str = MyClass.Value1;
// str == "something one"
}
}