我的枚举包含以下值:
private enum PublishStatusses{
NotCompleted,
Completed,
Error
};
Run Code Online (Sandbox Code Playgroud)
我希望能够以用户友好的方式输出这些值.
我不需要能够再次从字符串变为值.
在帖子Enum ToString中,描述了一个方法来使用自定义属性,DescriptionAttribute如下所示:
Enum HowNice {
[Description("Really Nice")]
ReallyNice,
[Description("Kinda Nice")]
SortOfNice,
[Description("Not Nice At All")]
NotNice
}
Run Code Online (Sandbox Code Playgroud)
然后,GetDescription使用如下语法调用函数:
GetDescription<HowNice>(NotNice); // Returns "Not Nice At All"
Run Code Online (Sandbox Code Playgroud)
但是,当我想简单地使用枚举值填充ComboBox时GetDescription,这并没有真正帮助我,因为我不能强制ComboBox调用.
我想要的是有以下要求:
(HowNice)myComboBox.selectedItem将返回所选值作为枚举值.NotNice,用户不会看到" Not Nice At All" 而是看到" ".显然,我可以为我创建的每个枚举实现一个新类,并覆盖它ToString(),但这对每个枚举来说都是很多工作,我宁愿避免这样做.
有任何想法吗?
从.Net 2.0泛型字典中获取密钥的值很容易:
Dictionary<int, string> greek = new Dictionary<int, string>();
greek.Add(1, "Alpha");
greek.Add(2, "Beta");
string secondGreek = greek[2]; // Beta
Run Code Online (Sandbox Code Playgroud)
但有没有一种简单的方法来获取价值的关键?
int[] betaKeys = greek.WhatDoIPutHere("Beta"); // expecting single 2
Run Code Online (Sandbox Code Playgroud) 我们注意到在C#(或Java)中开发的软件中存在很多错误会导致NullReferenceException.
有没有理由为什么"null"甚至被包括在语言中?
毕竟,如果没有"空",我就没有错,对吧?
换句话说,如果没有null,语言中的哪些功能无法工作?
我想在c#中从字符串到枚举之间进行明确的转换,以便得到:
(MyEnum) Enum.Parse(typeof(MyEnum),stringValue)
Run Code Online (Sandbox Code Playgroud)
我想把它驱逐到一个显式的强制转换操作符,我这样做但是没有用:
public static explicit operator (MyEnum)(value stringValue){
return (MyEnum) Enum.Parse(typeof(MyEnum),stringValue);
}
Run Code Online (Sandbox Code Playgroud)
你知道在C#中使用.NET 3.5是否可行?
可以使用这样的类(特定于设计/指南)吗?我正在使用MVVM模式.
public static class Pages
{
public const string Home = "Home.xaml";
public const string View2 = "View2.xaml";
/* a few more... */
}
Run Code Online (Sandbox Code Playgroud) 我有一个类似的枚举
Enum Complexity
{
NotSoComplex,
LittleComplex,
Complex,
VeryComplex
}
Run Code Online (Sandbox Code Playgroud)
我想在下拉列表中使用它,但不希望在列表中看到这样的Camel名称(对用户来说看起来很奇怪).相反,我希望有正常的措辞,如不那么复杂的小复杂(等)
此外,我的应用程序是多语言,我希望能够显示这些字符串本地化,我使用帮助器,TranslationHelper(字符串strID),它给我一个字符串ID的本地化版本.
我有一个工作的解决方案,但不是很优雅:我为枚举创建一个辅助类,其中一个成员Complexity和ToString()被覆盖,如下所示(代码简化)
public class ComplexityHelper
{
public ComplexityHelper(Complexity c, string desc)
{ m_complex = c; m_desc=desc; }
public Complexity Complexity { get { ... } set {...} }
public override ToString() { return m_desc; }
//Then a static field like this
private static List<Complexity> m_cxList = null;
// and method that returns the status lists to bind to DataSource of lists
public static List<ComplexityHelper> GetComplexities()
{
if (m_cxList == null) …Run Code Online (Sandbox Code Playgroud) 这可能是最好的例子.我有一个属性的枚举:
public enum MyEnum {
[CustomInfo("This is a custom attrib")]
None = 0,
[CustomInfo("This is another attrib")]
ValueA,
[CustomInfo("This has an extra flag", AllowSomething = true)]
ValueB,
}
Run Code Online (Sandbox Code Playgroud)
我想从实例中获取这些属性:
public CustomInfoAttribute GetInfo( MyEnum enumInput ) {
Type typeOfEnum = enumInput.GetType(); //this will be typeof( MyEnum )
//here is the problem, GetField takes a string
// the .ToString() on enums is very slow
FieldInfo fi = typeOfEnum.GetField( enumInput.ToString() );
//get the attribute from the field
return fi.GetCustomAttributes( typeof( CustomInfoAttribute ), false …Run Code Online (Sandbox Code Playgroud) 我以不同的方式找到了一个关于实现枚举的好例子.我认为这就是所谓的类型安全枚举模式.我开始使用它,但我意识到我不能在switch语句中使用它.
我的实现如下所示:
public sealed class MyState
{
private readonly string m_Name;
private readonly int m_Value;
public static readonly MyState PASSED= new MyState(1, "OK");
public static readonly MyState FAILED= new MyState(2, "ERROR");
private MyState(int value, string name)
{
m_Name = name;
m_Value = value;
}
public override string ToString()
{
return m_Name;
}
public int GetIntValue()
{
return m_Value;
}
}
Run Code Online (Sandbox Code Playgroud)
为了能够在C#的switch语句中使用这种模式,我可以添加到我的类中?
谢谢.
说我有一个类似的枚举:
enum OrderStatus
{
AwaitingAuthorization,
InProduction,
AwaitingDespatch
}
Run Code Online (Sandbox Code Playgroud)
我还在我的枚举上创建了一个扩展方法来整理UI中的显示值,所以我有类似的东西:
public static string ToDisplayString(this OrderStatus status)
{
switch (status)
{
case Status.AwaitingAuthorization:
return "Awaiting Authorization";
case Status.InProduction:
return "Item in Production";
... etc
}
}
Run Code Online (Sandbox Code Playgroud)
灵感来自这里的优秀帖子,我想SelectList用扩展方法将我的枚举绑定到一个:
public static SelectList ToSelectList<TEnum>(this TEnum enumObj)
但是,要在UI下拉列表中使用DisplayString值,我需要添加一个约束
: where TEnum has extension ToDisplayString
显然,除非有一些我不知道的聪明伎俩,否则这一切都不会对当前的方法起作用.
有没有人对我如何能够实现这样的事情有任何想法?
c# ×10
enums ×6
.net ×3
.net-3.5 ×1
attributes ×1
casting ×1
combobox ×1
interface ×1
java ×1
localization ×1
mvvm ×1
null ×1
reflection ×1
tostring ×1