我有以下枚举 - >
public enum SyncStatus
{
Unavailable = 0,
Checking = 5,
StartedAspNetDb = 10,
FinishedAspNetDb = 20,
StartedMatrixDb = 30,
FinishedMatrixDb = 40,
StartedConnectDb = 50,
FinishedConnectDb = 60,
StartedCmoDb = 70,
FinishedCmoDb = 80,
StartedMcpDb = 90,
FinishedMcpDb = 100
}
我在这里使用 - >
SyncInBackground.ReportProgress(SyncStatus.StartedAspNetDb);
MergeRepl aspnetdbMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "aspnetdb", "aspnetdb", "aspnetdb");
aspnetdbMergeRepl.RunDataSync();
SyncInBackground.ReportProgress(SyncStatus.FinishedAspNetDb);
SyncInBackground.ReportProgress(SyncStatus.StartedMatrixDb);
MergeRepl matrixMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "MATRIX", "MATRIX", "MATRIX");
matrixMergeRepl.RunDataSync();
SyncInBackground.ReportProgress(SyncStatus.FinishedMatrixDb);
SyncInBackground.ReportProgress(SyncStatus.StartedConnectDb);
MergeRepl connectMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "CONNECT", "Connect", "Connect");
connectMergeRepl.RunDataSync();
SyncInBackground.ReportProgress(SyncStatus.FinishedConnectDb);
我不明白为什么,如果int is default enum governing type
,我必须CAST
这一行,就像这样 - >
SyncInBackground.ReportProgress((int)SyncStatus.Checking);
原谅我的无知我只想了解事情的原因而不仅仅是因为它们是如此。
答案 0 :(得分:10)
没有从枚举类型到其基础类型的隐式转换。这使得意外使用枚举作为其数值变得更加困难。
(其他方面也没有转换。)
顺便说一句, 是从常量0到任何枚举类型的隐式转换。
哦,从盒装枚举值到其基础类型的拆箱 - 或反之亦然 - 也有效。至少在某一点上,CLI规范和C#规范都严重受损;它现在可能已被修复:)
编辑:
如果实际只想将值用作数字,则可以选择以下选项:
public static class SyncStatus
{
public const int Unavailable = 0;
public const int Checking = 5;
public const int StartedAspNetDb = 10;
public const int FinishedAspNetDb = 20;
public const int StartedMatrixDb = 30;
public const int FinishedMatrixDb = 40;
public const int StartedConnectDb = 50;
public const int FinishedConnectDb = 60;
public const int StartedCmoDb = 70;
public const int FinishedCmoDb = 80;
public const int StartedMcpDb = 90;
public const int FinishedMcpDb = 100;
}
或者,写一个这样的方法:
static void ReportProgress(SyncStatus status)
{
SyncInBackground.ReportProgress((int) status);
}