Display the type of enumeration defined in the base library

I am trying to revise my registration library. I'm stuck here. I use an enumeration, let him call ActionType to identify my operations, such as UserLogin, PurchaseOrder ... hundreds of them. And I use this type in my registration methods. But since I am sharing my journal library from my project code, for free communication, and the base library cannot access the ActionType defined in the project, how can I achieve this. To clarify this, let me explain the same case in java. Java allows enumerations to implement interfaces. So I could write:

In the base registrar library, I could define;

public interface IActionType {}

and in one of my several projects

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

So when I called my base library logger.log(ActionType.UserLogin, ....) , you get the main action. That would be enough. Is there anyway around this to accomplish this in C #? By the way, I was considering using IoC containers, but I need something more elegant.

Thanks so much for any help ...

+7
source share
3 answers

Here the log4net approach uses for the Level class (yes, this is a class, not an enumeration):

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

And interface

 public interface IActionType { int Value { get; } string Name { get; } } 

Using:

 logger.Log(ActionType.UserLogin); 
+3
source

The lazy beat me up, but I will send my decision anyway

 public void MyUsage(ITypesafeEnum myEnum) { Console.WriteLine(myEnum.Name); Console.WriteLine(myEnum.Val); } public interface ITypesafeEnum{ string Name{get;} int Val {get;} } public class TypesafeEnum:ITypesafeEnum{ public string Name {get;private set;} public int Val {get;private set;} private TypesafeEnum(){} private TypesafeEnum(string name, int val){ Name = name; Val = val; } public static readonly TypesafeEnum Bedroom = new TypesafeEnum("Bedroom", 1); public static readonly TypesafeEnum LivingRoom = new TypesafeEnum("Living Room",2); } 
+3
source

Here is another approach that generics uses:

 public void Log<EnumType>(EnumType enumMember) { var name = enumMember.ToString(); int value = (int)(object)enumMember; Console.WriteLine(name + " = " + value); } 

Call the above method as follows:

 Log<ActionType>(ActionType.UserLogin); Log<ActionType>(ActionType.PurchaseOrder); 

The results in the output file are as follows:

 UserLogin = 0 PurchaseOrder = 1 
+1
source

All Articles