避免输入完全限定名称
Avoiding typing fully qualified name
假设我有一个帮手 class,像这样:
namespace MyNamespace
{
public class DataHelpers
{
public string SysnamePath ( string db , string schema , string table )
{
return "[" + db + "].[" + schema + "].[" + table + "]";
}
}
}
我该如何重写这个或者我该怎么做才能使我不必一直输入完全限定的名称...
字符串 dbpath = DataHelpers.SysnamePath(...);
我不希望它成为扩展方法,或者我不明白这有什么帮助,我不想将该方法粘贴到与调用方相同的页面中。
有可能我不明白一些基本的东西,因为我正在自学。我的理解是该方法必须在 class 中。我只是不想每次都输入 class 这个名字。
就像我说的,我确定这是基本的东西。
使您的 class 和方法静态化:
namespace MyNamespace
{
public static class DataHelpers
{
public static string SysnamePath ( string db , string schema , string table )
{
return "[ " + db + " ].[ " + schema + " ].[ " + table + " ]";
}
}
}
在文件头中添加你想使用它的地方:
using static DataHelpers;
使用:
string dbpath = SysnamePath(...);
在您的命名空间中添加:
using DH = MyNamespace.DataHelpers
那么你可以这样做:
var p = new DH();
假设我有一个帮手 class,像这样:
namespace MyNamespace
{
public class DataHelpers
{
public string SysnamePath ( string db , string schema , string table )
{
return "[" + db + "].[" + schema + "].[" + table + "]";
}
}
}
我该如何重写这个或者我该怎么做才能使我不必一直输入完全限定的名称...
字符串 dbpath = DataHelpers.SysnamePath(...);
我不希望它成为扩展方法,或者我不明白这有什么帮助,我不想将该方法粘贴到与调用方相同的页面中。
有可能我不明白一些基本的东西,因为我正在自学。我的理解是该方法必须在 class 中。我只是不想每次都输入 class 这个名字。
就像我说的,我确定这是基本的东西。
使您的 class 和方法静态化:
namespace MyNamespace
{
public static class DataHelpers
{
public static string SysnamePath ( string db , string schema , string table )
{
return "[ " + db + " ].[ " + schema + " ].[ " + table + " ]";
}
}
}
在文件头中添加你想使用它的地方:
using static DataHelpers;
使用:
string dbpath = SysnamePath(...);
在您的命名空间中添加:
using DH = MyNamespace.DataHelpers
那么你可以这样做:
var p = new DH();