C# using alias as type parameter in other using alias

人走茶凉 提交于 2020-05-08 09:42:51

问题


I'm trying to define a pair of type aliases at the top of my C# program. This is a short example of what I'm trying to do:

using System;
using System.Collections.Generic;

namespace Foo {
    using TsvEntry = Dictionary<string, string>;
    using Tsv = List<TsvEntry>;
}

When I try to compile this using mcs 3.2.8.0, I get the following error message:

foo.cs(6,19): error CS0246: The type or namespace name `TsvEntry' could not be found. Are you missing an assembly reference?

Is it possible to use using aliases within other aliases in C#, or am I missing something about the way using statements work?


回答1:


Check documentation for this question:

https://msdn.microsoft.com/en-us/library/aa664765(v=vs.71).aspx

It says:

The order in which using-alias-directives are written has no significance, and resolution of the namespace-or-type-name referenced by a using-alias-directive is not affected by the using-alias-directive itself or by other using-directives in the immediately containing compilation unit or namespace body. In other words, the namespace-or-type-name of a using-alias-directive is resolved as if the immediately containing compilation unit or namespace body had no using-directives. In the example

namespace N1.N2 {}
namespace N3
{
   using R1 = N1;         // OK
   using R2 = N1.N2;      // OK
   using R3 = R1.N2;      // Error, R1 unknown
}

the last using-alias-directive results in a compile-time error because it is not affected by the first using-alias-directive.

Technically, you cannot do it same namespace, but if you do alias in namespace 1, and do alias for this alias in a nested namespace, it will work:

namespace N1
{
    namespace N12 { }
}

namespace N2
{
    using R1 = N1;

    namespace N2
    {
        using R2 = R1.N12;
    }
}

I am not really sure it's worth using aliases in your specific example, consider using them as rare as you can, mostly for resolving namespace conflicts.




回答2:


From MSDN's Using alias directives :

The order in which using-alias-directives are written has no significance, and resolution of the namespace-or-type-name referenced by a using-alias-directive is not affected by the using-alias-directive itself or by other using-directives in the immediately containing compilation unit or namespace body. In other words, the namespace-or-type-name of a using-alias-directive is resolved as if the immediately containing compilation unit or namespace body had no using-directives.

// emphasis mine

In your example, the last using-alias-directive results in a compile-time error because it is not affected by the first using-alias-directive.




回答3:


Just as an addition to the other answers.

If you really want to continue your way, nesting the namespaces would be the way to go. Since for the second namespace, the first using would then be defined.

The following would work since you are in a deeper namespace:

namespace N1
{
    using TsvEntry = Dictionary<string, string>;

    namespace N1.N2
    {
        using Tsv = List<TsvEntry>;

    }
}

Please note that although it is working, i wouldn't recommend using this kind of code structure since you can end up with a deeply nested code.




回答4:


As others have mentioned, using Alias this way is not possible, however, you can get a similar functionality using class inheritance.

Using your example, you could do something like this:

using System;
using System.Collections.Generic;

namespace Foo {
    class TsvEntry : Dictionary<string, string> {}
    class Tsv : List<TsvEntry> {}
}

This has the additional advantage that you can easily add extra functionality to these classes later on as you need it.




回答5:


(Adding this here because I ran across this question a lot when searching for a related issue, so it might help others)

If you're looking to use aliases to functions for a functional style of programming, what you're looking for is the delegate keyword. You can think of delegate as making an interface, except it's for a function.

so instead of using Expression = Func<string>; using Converter = Func<List<string>, Expression>; you do delegate string Expression(); delegate Expression Converter(List<string> tokenizedExpressions);

https://weblogs.asp.net/dixin/functional-csharp-function-type-and-delegate



来源:https://stackoverflow.com/questions/35921803/c-sharp-using-alias-as-type-parameter-in-other-using-alias

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!