JL.*_*JL. 5 c# resources enums
使用VS2010和.net V4.0我想实现以下目标:
我的项目中已有2个资源文件用于2种语言 - 英语和捷克语.
我必须说.net中的资源管理非常好,我很惊讶甚至在实现String时获得代码完成,例如:
string desc = Strings.ResourceManagerDesc
Run Code Online (Sandbox Code Playgroud)
这将获得与线程的当前文化相关联的字符串.
现在我正在尝试创建一个Enum,它可以从字符串资源中解释出Enum的String部分.按以下方式(此代码不起作用):
public enum DownloadStatus
{
1 = Strings.DownloadState_Complete,
2 = Strings.DownloadState_Failed,
3 = Strings.DownloadState_InProgress
}
Run Code Online (Sandbox Code Playgroud)
这是一个组成的例子,但你可以在这里看到这一点.由于上面的代码不起作用,有没有最好的实践方法来实现我想要的?
这个问题很老但没有接受的答案我正在寻找一个好方法来做到这一点.
这就是我所做的,我为我的Enum构建了一个扩展,以便它从资源管理器返回一个值:
public enum EventType
{
NewVersion = 1,
Accepted = 2,
Rejected = 3,
BruteForce = 4
}
public static class EventTypeExtension
{
public static string Display(this EventType type)
{
return Strings.ResourceManager.GetString("EventType_" + type);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助别人!