'local::' ns prefix to refer to project's default namespace #802
Replies: 6 comments
-
There is no "default namespace" to the C# compiler. The project option exists only for tooling in Visual Studio and the build system. I could see using |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Fair comments. Although I'm not sure I buy the argument that Equally - yes, of course I can add an alias instead to sidestep the problem. But the same is true of the As a 'standard' alias, though - it seems to me something that could be achieved using a source file rewrite as part of Roslyn's compilation process (although I confess I don't even know if that's possible right now): A property fed into the compilation containing one or more namespaces with aliases which are then automatically rewritten into the parsed code file's |
Beta Was this translation helpful? Give feedback.
-
I only say |
Beta Was this translation helpful? Give feedback.
-
I don't think it would be dirty. You can already name those aliases whatever you want per reference and that naming is external to the source. The "global" alias just happens to be the default. I don't really see an issue adding a "local" alias as well that would restrict resolution specifically to the current assembly as long as it wasn't used to alias some other reference. But that alias the namespaces themselves, just the assembly: using FooProduct = foo::Company.Product;
using BarProduct = bar::Company.Product;
using MyProduct = local::Company.Product; |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Right. If the full namespace is contained in each file, I have no problem with it. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This suggestion stems from a build failure I encountered today:
In
Company.Product/Extensions.cs
:And in
Company.Product/Foo.cs
:And the referenced
Company.Common
project declares:So we have a common library with the namespace
Company.Common.Foo
, and another projectCompany.Product
that declares a set of extension methods which merge directly intoCompany.Common.Foo
, but which reference a classFoo
that is defined in that project.The compiler error is that the namespace
Company.Common.Foo
doesn't contain a typeBar
. And of course it doesn't :)I know this is not a bug - but the fix is either to alias
Company.Product.Bar
or to namespace-qualify it on the offending line asvar a = Company.Product.Bar(obj);
Proposal
A
local::
namespace prefix which acts as an alias to the default namespace of whichever project the code file is being compiled within.The above error could then be fixed with
var a = local::Bar(obj);
Beta Was this translation helpful? Give feedback.
All reactions