Search code examples
c#.netdllnamespaces

Make a intern enum visible by API namespace


I am writing an API for an existing software project. My API often only works as a wrapper and sends function calls directly to the logic of the software. Of course I want to avoid to write duplicated code. Therefor my Question is how to make a internal enumeration/class/etc visible for a user who is using the API.

I will explain it on a small example:

internal logic:

namespace internlogic{
  public class Log {
    public enum Level = {All, Debug, Error, Fatal);

    public static SetLogLevel(Level lvl){
       ...
    } 
  }
}

API:

namespace API {
  public class APILog {

   public SetLogLevel(internlogic.Level lvl)
     internlogic.Log.SetLogLevel(lvl);
   }
}

The external User is now using ONLY the API-namespace and I am looking for a possiblitiy he can use the enum level. Is it possible to include this enum into the API namespace without writing "wrapper enum"? I want to avoid that the user has to include a lot of diffrentnamespaces.

external API-user:

using namespace API;

...
Level loglvl = Level.Debug //Its not possible because Level is not in the API namespace
APILog.setLogLevel(loglvl);
...

Until now the there is no access control for the internallogic implemented.


Solution

  • Just for having an example. This is what i meant in my comment, by making an API-LogLevel-Enum:

    namespace API
    {
        public enum LogLevel
        {
            All, Debug, Error, Fatal
        }
    
        internal static class LogLevelExtension
        {
            internal static internlogic.Log.Level GetLevel(this LogLevel level)
            {
                switch (level)
                {
                    case LogLevel.All:
                        return internlogic.Log.Level.All;
                    case LogLevel.Debug:
                        return internlogic.Log.Level.Debug;
                    case LogLevel.Error:
                        return internlogic.Log.Level.Error;
                    case LogLevel.Fatal:
                        return internlogic.Log.Level.Fatal;
                    default:
                        throw new Exception("This shoudn't happen! : )");
                }
            }
        }
    }
    

    You could use it like this:

    public void DoSomething(LogLevel level)
    {
       internlogic.Log.Level internLevel = level.GetLevel();
       ...
    }