使用静态类型,而不是变量 [英] Use static type instead of variable

查看:114
本文介绍了使用静态类型,而不是变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法告诉编译器使用静态类型,而不是一个变量时,您的项目不使用命名空间?

Is there any way to tell the compiler to use a static type instead of a variable when your project doesn't use namespaces?

比如说,我有一个类调用用户的各种静态和非静态方法。比方说,静态方法被调用的getUser()

For instance, I have a class called User with various static and non-static methods. Let's say one of the static methods is called GetUser().

我试图调用 User.GetUser()从还具有范围称为用户变量(从基类继承)的方法方法。然而,编译器会抱怨说,它无法找到 User.GetUser(),因为它认为我指的是用户变量的范围。

I'm trying to call that User.GetUser() method from a method that also has a variable in scope (inherited from base class) called User. However, the compiler complains saying that it can't find User.GetUser() because it thinks I'm referring to the User variable that's in scope.

如果这个项目中使用的命名空间,我只是做 ns1.User.GetUser(),但是这并不可行在这种情况下。有没有一种方法,我可以告诉大家,我指的是在用户的编译器键入用户变量代替?

If this project used namespaces, I could just do ns1.User.GetUser(), but that's not feasible in this case. Is there a way that I can tell the compiler that I'm referring to the User type instead of the User variable?

推荐答案

您可以使用:

global::User.GetUser()

或使用指令别名类型:

Or a using directive to alias the type:

using UserType = User;

...

UserType.GetUser();



我强烈建议你使用,虽然命名空间:)

I'd strongly encourage you to use namespaces though :)

这篇关于使用静态类型,而不是变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆