公开基础库中定义的枚举类型

ayk*_*ayk 7 c# enums interface

我正在尝试修改我的日志库.这是我被困的地方.我使用枚举,让我们称之为ActionType,以识别我的操作,如UserLogin,PurchaseOrder ......数百个.我在记录器方法中使用这种类型.但是,由于松散耦合和基础库无法访问项目中定义的ActionType,我从我的项目特定代码中分离我的记录器库,我该如何实现这一点.为了澄清它,让我在java中解释相同的情况.Java允许枚举实现接口.所以我可以写:

在基本记录器库中,我可以定义;

public interface IActionType {}

在我的几个项目之一

public enum ActionType implements IActionType {UserLogin, PurchaseOrder, .....}

因此,当我调用我的 logger.log(ActionType.UserLogin, ....)基础库时,将获得基础操作.这一切都足够了.在c#中有没有在它周围完成这个?顺便说一句,我考虑使用IoC容器,但我想要更优雅的东西.

非常感谢任何帮助......

Ser*_*kiy 3

这是 log4net 用于Level类的方法(是的,它是类,而不是枚举):

public class ActionType : IActionType
{
   public static readonly ActionType UserLogin;
   public static readonly ActionType PurchaseOrder;

   static ActionType()
   {
       UserLogin = new ActionType(1, "User Login");
       // ...
   }

   public ActionType(int value, string name)
   {           
       // verify arguments values
       Value = value;
       Name = name;
   }

   public int Value { get; private set; }
   public string Name { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)

及接口

public interface IActionType
{
    int Value { get; }
    string Name { get; }
}
Run Code Online (Sandbox Code Playgroud)

用法:

logger.Log(ActionType.UserLogin);
Run Code Online (Sandbox Code Playgroud)

  • 详细信息:实例构造函数中太多了“class”一词。那么静态构造函数需要括号。 (2认同)