Assume in a winform application we have in program.cs
:
static class Program
{
public static SomeClass someClass;
[STAThread]
static void Main()
{
//something
}
}
so to use someClass开发者_高级运维
in other classes in current Project we should access Program.someClass
the question is can we make an alias for Program.someClass
like for example c1
and use c1
in code instead of Program.someClass
?
If you're just looking to alias it for wrist-friendliness, do it as an inline delegate, like this:
Func<SomeClass> c1 = () => Program.someClass;
But I agree that it makes your code less clear in most cases.
Assuming that you want to achieve something like macros in C/C++ and also want to get rid of "Program" as naming scope, I think you cannot do that in C#.
Just pass that object to each class that need to work with it
精彩评论