C#可以访问没有完全限定名称的枚举

我有一个C#枚举类型,最终有很长的限定名。 例如

DataSet1.ContactLogTypeValues.ReminderToFollowupOverdueInvoice. 

为了便于阅读,如果我能告诉某个特定的函数只使用名称的最后一部分,那会很好…

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

是否有可能在C#中做这样的事情?

您可以使用using指令指定别名。 它将存在于文件的任何位置,但不是在一个特定的方法中。

我意识到这可能不是你想象的解决方案,但它确实允许你编写你要求的代码。

 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; } 

从C#6开始,您可以使用using static

 using static DataSet1.ContactLogTypeValues; ... logtype = ReminderToFollowupOverdueInvoice; ... 

有关详细信息,请参阅https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-static 。