是否有可能引用静态类的方法,而不引用类? [英] Is it possible to reference a method in a static class without referencing the class?

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

问题描述

是否有这样的声明,该方法能够直接而无需参照它的类名来解决一类/命名空间层次结构内的静态方法的方法吗?例如,如果我有这样的命名空间和类...

 命名空间UsefulMethods
{
    公共静态类DataBaseStuff
    {
        公共静态数据表GetDataTable(字符串命令,康涅狄格州的SqlConnection)
        {
            DT =新的DataTable();
            做东西...
            返回DT;
        }
    }
}

然后我写使用这个类中的一些code,我把声明...

 使用UsefulMethods;

...我的code的顶部,我可以得到一个DataTable中返回给我的...

 的DataTable myDataTable = DataBaseStuff.GetDataTable(指挥,康涅狄格州);

我想也许我可以指一个静态类,using语句,以及像...

 使用UsefulMethods.DataBaseStuff;

...所以我可以只参照方法,无需在类名像这样...

 的DataTable myDataTable = GetDataTable(指挥,康涅狄格州);

...但是这似乎并没有工作。该智能感知没有找到在using语句静态类,所以我想这只是工程的命名空间。那么,有没有一些办法,我有我的code知道静态类我指的是没有我每次调用静态方法一次引用它?

感谢您的阅读。


解决方案

通过 C#6.0 你可以做到这一点的静态导入

 使用静态UsefulMethods.DataBaseStuff;

在那之前你不能。您必须使用类名与静态方法。

请参阅:新的语言功能在C#6


  

使用静态


  
  

该功能允许所有类型的访问静态成员是
  原装进口,使它们可不会在随后的资格
  code:


 使用静态System.Console;
使用静态System.Math;
使用静态System.DayOfWeek;
类节目
{
    静态无效的主要()
    {
        的WriteLine(SQRT(3 * 3 + 4 * 4));
        的WriteLine(星期五 - 星期一);
    }
}


  

这当你有一组相关的功能是伟大的
  你用所有的时间某个域。 System.Math将是一个
  那常见的例子。它也可以让你直接指定
  枚举类型的个人命名的值,如System.DayOfWeek
  成员之上。


Is there a way to declare a static method within a class/namespace hierarchy such that the method can be addressed directly without having the refer to its classname? For example if I have this namespace and class...

namespace UsefulMethods
{
    public static class DataBaseStuff
    {
        public static DataTable GetDataTable(String command, SqlConnection conn)
        {
            dt = new DataTable();
            dostuff...
            return dt;
        }
    }
}

then I write some code that uses this class and I put statement...

using UsefulMethods;

...at the top of my code, I can get a DataTable returned to me by...

DataTable myDataTable = DataBaseStuff.GetDataTable(command, conn);

I thought maybe I could refer to a static class with a using statement as well like...

using UsefulMethods.DataBaseStuff;

...so that I could just refer to the method without the class name like this...

DataTable myDataTable = GetDataTable(command, conn);

...but this doesn't seem to work. The intellisense doesn't find the static class in the using statement, so I guess that just works for namespaces. So, is there some way for me to have my code to know what static class I'm referring to without referencing it every time I call one of the static methods?

Thank you for reading.

解决方案

With C# 6.0 you can do that with static import

using static UsefulMethods.DataBaseStuff;

Until then you can't. You have to use class name with static method.

See: New Language Features in C# 6

Using static

The feature allows all the accessible static members of a type to be imported, making them available without qualification in subsequent code:

using static System.Console;
using static System.Math;
using static System.DayOfWeek;
class Program
{
    static void Main()
    {
        WriteLine(Sqrt(3*3 + 4*4)); 
        WriteLine(Friday - Monday); 
    }
}

This is great for when you have a set of functions related to a certain domain that you use all the time. System.Math would be a common example of that. It also lets you directly specify the individual named values of an enum type, like the System.DayOfWeek members above.

这篇关于是否有可能引用静态类的方法,而不引用类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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