关于 enum 的更优雅的设计
我正在学习C#。
我听说 C# 是最具可构建性的语言之一。那么你们能让我的代码更加优雅和高效吗?
public class ISO639
{
public enum ISO639Code
{
Afrikaans, //af
Albanian, //sq
Amharic, //am
...
Yiddish, //yi
Unknown
}
public static string GetISO639CodeString(ISO639.ISO639Code l)
{
switch (l)
{
case ISO639Code.English: return "en";
case ISO639Code.Japanese: return "ja";
...
case ISO639Code.Hebrew: return "he";
default: return "";
}
public static ISO639.ISO639Code GetISO39CodeValue(string s)
{
switch (s)
{
case "ko" : return ISO639Code.Korean;
case "en" : return ISO639Code.English;
...
case "hu" : return ISO639Code.Hungarian;
default: return ISO639Code.Unknown;
}
}
}
这是我的班级 ISO639。此类为 ISO639 代码提供枚举,但我需要从 ISO639 枚举到纯字符串的类型转换。 (例如 ISO639.ISO639Code.Italian =>“它”)。我还需要从纯字符串到 ISO639 枚举的类型转换。 (例如“it”=> ISO639.ISO639Code.Italian)。
有没有更有效的编码风格?
I'm on learning for C#.
I heared C# is one of the most constructible language. so would you guys make my code more elegant and efficient?
public class ISO639
{
public enum ISO639Code
{
Afrikaans, //af
Albanian, //sq
Amharic, //am
...
Yiddish, //yi
Unknown
}
public static string GetISO639CodeString(ISO639.ISO639Code l)
{
switch (l)
{
case ISO639Code.English: return "en";
case ISO639Code.Japanese: return "ja";
...
case ISO639Code.Hebrew: return "he";
default: return "";
}
public static ISO639.ISO639Code GetISO39CodeValue(string s)
{
switch (s)
{
case "ko" : return ISO639Code.Korean;
case "en" : return ISO639Code.English;
...
case "hu" : return ISO639Code.Hungarian;
default: return ISO639Code.Unknown;
}
}
}
Here is a my class ISO639. This class provides enum for ISO639 code, but I need a type conversion on from ISO639 enum to plain string. (ex. ISO639.ISO639Code.Italian => "it"). I also need a type conversion from plain string to ISO639 enum. (ex. "it" => ISO639.ISO639Code.Italian).
Is there a more efficient coding style for that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以添加标准 System.ComponentModel。每个枚举条目的描述属性,然后读取它。
编辑:
You can add standard System.ComponentModel.Description attribute to each enum entry and then read it.
EDIT:
当然。您可以使用属性:
Sure. You can use attributes:
使用字典,例如:
new Dictionary
。Use dictionary, for example:
new Dictionary<ISO639Code, string>
.我建议您对枚举使用 C# 扩展方法,它们允许您添加所需的任何逻辑。
例如,请参阅 http://pietschsoft.com /post/2008/07/c-enhance-enums-using-extension-methods.aspx
I suggest you to use C# extension methods to enums, they allow you to add whatever logic you want.
For example, see http://pietschsoft.com/post/2008/07/c-enhance-enums-using-extension-methods.aspx
我只是将信息存储在类似字典的对象中。这样你就可以通过键引用名称并直接获取值。
I'd simply store the information in a dictionary-like object. This way you can reference the name by key and get the value directly.
您有一个枚举:
等。
创建一个数据库表:
其中 ISO639Id 映射到枚举的值。
在代码中,您需要一个包含从数据库读取的 Id 和 Code 值的 ISO630 类。
(您可以加载一次,然后将其缓存在内存中。)
这种方法的优点在于它可以轻松扩展,这样如果将来您想为每个 ISO639 代码存储更多信息,您只需添加另一个字段即可。
You have an enum:
etc.
Create a database table:
Where the ISO639Id maps to the value of the enum.
In code you'd want a ISO630 Class containing Id and Code values read from the database.
(You can load this once and then cache it in memory.)
The beauty of this approach, is it can be easily extended so that if in future you wanted to store more pieces of information for each ISO639 code, you could simply add another field.
查看 System.Globailzation 命名空间。您需要的功能看起来已经在那里实现了。在最坏的情况下,您可以看到 .Net 框架中应用的架构和技术来解决非常相似的问题。
Look at System.Globailzation namespace. The functionality you require looks to be already implemented there. At worst you can see the architecture and technique applied in the .Net framework to solve a very similar problem.
枚举非常适合在代码中使用,因为它们确实是强类型的,并且使重构变得更容易。
请按照下列步骤操作:
使用属性来表示要附加到枚举的任何额外信息。通常这是一个简单的
Description
属性。像这样的东西:公共枚举 IsoCode
{
[描述(“af”)]
非洲人 = 0,
[描述(“上午”)]
美国人=1
}
编写一些扩展方法来将字符串和整数与此枚举相互转换:
现在可以根据需要使用枚举。
Enumerations are really good to work in code, as they are really strongly typed and make refactoring easier.
Follow these steps:
Use attributes for whatever extra information you want to attach to an enum. Usually this is a simple
Description
attribute. Something like:public enum IsoCodes
{
[Description("af")]
Africans = 0,
[Description("am")]
Americans = 1
}
Then write some extension methods to convert strings and integers to and from this enum:
Now use your enums as you like.
我会选择使用
ISO639Code
作为类而不是枚举:然后使用包含所有可能代码的全局
List
,并根据代码名称或值查找特定代码,只需在列表中搜索即可。就我个人而言,我更喜欢这样做而不是调整枚举。
I would go with having
ISO639Code
as class instead of enum:Then have global
List<ISO639Code>
with all possible codes, and to find specific code based on code name or value, just search for this in the List.Personally, I prefer this over tweaking the enum.