问题
I have a question about the using
statement for multiple files at once.
I have created an overload for a class that I want to use in my program. To do so in one of my files I have added the following using statement.
using ClassName = CustomClassName;
This works, but only for that particular file.
Is there a way to get this to work for my entire project?
回答1:
This is called type aliasing, re-utilizing the using
keyword may be confusing, but it is not an import.
The purpose of this statement is to make a certain class accessible via a different name. This is useful in cases when you have different assemblies linked to your project which accidentally have classes with the same name.
If you for instance have A.dll
that defines class Foo
under the A
namespace, and a B.dll
assembly that also defines a Foo
class under the B
namespace, you can use:
using FooA = A.Foo;
using FooB = B.Foo;
to make distinctions between both.
The scope of this is usually the current file, although, if you happen to define multiple namespaces in the same file, you can scope that to the namespace within the file:
using FooA = A.Foo;
namespace N1
{
// knows about FooA;
using FooB = B.Foo;
}
namespace N2
{
// knows about FooA
// does not know about FooB
}
Practically you can make this aliasing more defined but no broader than the file's scope.
回答2:
No.
using
directives are per file.
You can always create a template that includes it.
回答3:
No; C# does not have any such feature.
回答4:
A simple solution for this is to create a template and use it into you project or class. this will give you a way to use the desire usings and directive as you wish.
here is a good sample to create a template .
http://www.rhyous.com/2010/02/17/how-to-modify-the-default-new-class-template-in-visual-studio-2008/
http://visualstudiomagazine.com/articles/2008/09/01/define-your-own-item-templates.aspx
来源:https://stackoverflow.com/questions/16918702/c-sharp-using-statement-application-scope