diiN__________
diiN__________

Reputation: 7656

Define global alias for static classes

I have a static ExceptionHelper that looks like this:

public static class ExceptionHelper
{
    public static async void ShowDialog(string message)
    {
        // Show message
    }
}

Whenever I want to call this method I do it like this at the moment:

ExceptionHelper.ShowDialog("This is a message.");

I now thought of defining an alias for the ExceptionHelper to not having to write the whole word each time I want to use it.

I know I can achieve it with using:

using Ex = MyNamespaces.ExceptionHelper;

But then I'd have to define it in each file I want to use the method. Is there a way I can define the alias globally without changing the name of the class? Or is there any attribute I can set above the class declaration?

Upvotes: 1

Views: 8232

Answers (4)

György Kőszeg
György Kőszeg

Reputation: 18013

In C# 6.0 you can use static usings:

using static MyNamespace.ExceptionHelper;

Of course not globally, that works only for defines. But in a file where you use this line, you can use the members of the ExceptionHelper without any prefix.


Update:

Well, my original answer didn't age well. Starting with C# 10.0 global usings became available. It can be combined also with static usings so my original answer can be updated like this:

global using static MyNamespace.ExceptionHelper;

It can be placed in any file in the project but it's recommended to create a dedicated file for the global usings.

Alternatively, you can specify the global usings in the .csproj file:

<ItemGroup>
    <Using Include="MyNamespace.ExceptionHelper" Static="True"/>
</ItemGroup>

If ImplicitUsings is enabled in the .csproj file, then the line above will include your custom using in the same obj/netversion/GlobalUsings.g.cs file where the default usings also reside.

Upvotes: 4

Kaddyuk
Kaddyuk

Reputation: 107

As of C# 10, you can now define gloabl usings.

// GlobalUsing.cs
global using static WhateverNamespaceExceptionHelperLivesIn.ExceptionHelper;

And it will now be available globally, without having to define the class name, or the namespace, at the top of each class.

// Available Globally in the Project
public class TestClass
{
    public void TestMethod()
    {
        ShowDialog("This is a message");
    }
}

Upvotes: 6

Stokely
Stokely

Reputation: 15809

This might apply, even though you are using a method. You could use an ENUM type instead that lies outside of any namespace and access globals values that way. Place the enum in a file outside of any namespace. You can access it globally that way, or if you have trouble, using the "global" keyword below if you have any trouble referencing it:

enum Size
{
SMALL = 1,
MEDIUM = 5,
LARGE = 10
}

class Test {
    int mysize1 = (int)Size.SMALL;
    int mysize2 = (int)global::Size.MEDIUM;
}

Upvotes: 0

RB.
RB.

Reputation: 37192

Extension Method

You could make it an extension method on string.

public static class ExceptionHelper
{
    public static async void ShowDialog(this string message)
    {
        // Show message
    }
}

Then you would use it like so:

using WhateverNamespaceExceptionHelperLivesIn;

public class TestClass
{
    public void TestMethod()
    {
        "This is a message".ShowDialog();
    }
}

This makes your question moot - you don't have to define an alias at all.

Static imports

An alternative approach is to import the class statically. You won't need an alias, because you can reference the ShowDialog method directly. This will require C#6/Visual Studio 2015.

using static WhateverNamespaceExceptionHelperLivesIn.ExceptionHelper;

public class TestClass
{
    public void TestMethod()
    {
        ShowDialog("This is a message");
    }
}

Upvotes: 5

Related Questions