0

I am making a C# Library with only 1 class, that should act as a shell for some web services.

Those services have their own classes and enums.

In some of my functions, some parameters should be enums from the web service (its easiest to do it like that).

But, when I need to call that function from the code that is using the library in question, it ends up something like this:

LibraryClass.FunctionName(LibraryName.WebServicesName.EnumName.EnumValue);

Is it possible to make the code look more like this:

LibraryClass.FunctionName(EnumName.EnumValue);

by exposing the enum from web services in the library class?

I know I can do this by creating my own Enum in library class, and I will probably do that, but this questions crossed my mind, and I stopped here to ask: is it possible to expose enums like this?

maccettura
  • 10,514
  • 3
  • 28
  • 35
Monset
  • 648
  • 5
  • 25
  • 1
    _using LibraryName.WebServicesName;_ ??? – Steve May 15 '18 at 16:51
  • 1
    Have you tried `using EnumName = LibraryName.WebServicesName.EnumName;` ? – Aleks Andreev May 15 '18 at 16:51
  • @Steve yes. Read carefuly. LibraryName is the library that im working on, that is included on another project. WebServiceName is the name of the web service that is inside that library. – Monset May 15 '18 at 16:55
  • @AleksAndreev This works fine. I will have this in mind, I didnt know something like this existed. Thank you. It's just not what I asked for, but very close :) – Monset May 15 '18 at 17:02
  • Note that, unless I'm missing something, the clients of your `LibraryClass` will then have to pass a `WebServiceName.EnumName.EnumValue` instead of a `LibraryClass.EnumName.EnumValue` to your methods that require them. – Rufus L May 15 '18 at 17:05

1 Answers1

2

With the new using static added in C# 6 what you want can easily be done.

using static LibraryName.WebServicesName;

This will allow you to import the static members of the Library.WebServicesName class and its nested classes and enums in to your own namespace.

Example usage

using System;
using static LibraryName.WebServicesName;

public class Program
{
    public static void Main()
    {
        Console.WriteLine(EnumName.Foo);
    }
}

namespace LibraryName
{
    class WebServicesName{

        public enum EnumName { Foo, Bar}
    }
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431