C#:property/field namespace ambiguities

cod*_*nix 1 c# namespaces properties syntax-error collision

我得到编译错误,因为编译器认为Path.Combine引用了我的字段,但我希望它引用类System.IO.Path.除了总是必须编写类似System.IO.Path.Combine()的FQN之外,还有一种处理这个问题的好方法吗?

using System.IO;

class Foo
{
   public string Path;

   void Bar(){ Path.Combine("",""); } // compile error here
}
Run Code Online (Sandbox Code Playgroud)

Pra*_*yan 5

你可以这样做:

using IOPath = System.IO.Path;
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中:

class Foo
{
   public string Path;

   void Bar(){ IOPath.Combine("",""); } // compile error here
}
Run Code Online (Sandbox Code Playgroud)