簡體   English   中英

C#可以訪問沒有完全限定名稱的枚舉

[英]C# can I access an enum without full qualified names

我有一個C#enum類型,最終有很長的限定名。 例如

DataSet1.ContactLogTypeValues.ReminderToFollowupOverdueInvoice.

為了便於閱讀,如果我能告訴某個特定的函數只使用名稱的最后一部分,那會很好...

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

是否有可能在C#中做這樣的事情?

從C#6開始,您可以使用using static

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

有關詳細信息,請參閱https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-static

您可以使用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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM