C#我能否在不使用完全限定名称的情况下访问枚举?

11

我有一个 C# 枚举类型,其限定名称非常长。例如:

DataSet1.ContactLogTypeValues.ReminderToFollowupOverdueInvoice.

为了提高可读性,如果我可以指定一个特定的函数仅使用名称的最后一部分,就像...

{
    using DataSet1.ContactLogTypeValues;
    ...
    logtype = ReminderToFollowupOverdueInvoice;
    ...
}

在 C# 中是否有类似这样的功能可供使用?


我不这么认为,但你可以在类外定义枚举,这样至少就可以跳过 DataSet1. 部分。 - Pierre-Luc Pineault
1
使用别名进行结帐:http://msdn.microsoft.com/zh-cn/library/c3ay4x3d(v=vs.110).aspx - Bob Horn
5
您无法为实际的枚举创建别名,但可以为枚举类型创建别名,方法如下: using ContactLogTypeValues = YourNamespace.DataSet1.ContactLogTypeValues; 。有关更多信息,请参阅http://msdn.microsoft.com/en-us/library/aa664765(v=vs.71).aspx - Kirill Shlenskiy
3个回答

15

5

您可以使用using指令来指定别名。它将存在于整个文件中,而不是某个特定的方法中。


谢谢,别名非常接近我想要的。 - user1961169

3

我知道这可能不是你想要的解决方案,但它可以让你编写所需的代码。

enum ContactLogTypeValues
{
    ReminderToFollowupOverdueInvoice,
    AnotherValue1,
    AnotherValue2,
    AnotherValue3
};

static ContactLogTypeValues ReminderToFollowupOverdueInvoice = ContactLogTypeValues.ReminderToFollowupOverdueInvoice;
static ContactLogTypeValues AnotherValue1 = ContactLogTypeValues.AnotherValue1;
static ContactLogTypeValues AnotherValue2 = ContactLogTypeValues.AnotherValue2;
static ContactLogTypeValues AnotherValue3 = ContactLogTypeValues.AnotherValue3;

static void Main(string[] args)
{
    var a = ReminderToFollowupOverdueInvoice;

}

1
是的,我考虑过这种方法,但真的不想走那条路。 :) 这确实使函数代码看起来很好,但代价是一堆无用的变量混乱。 - user1961169

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接