C#:property / field namespace ambiguities

时间:2009-06-22 17:34:51

标签: c# properties namespaces 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
}

2 个答案:

答案 0 :(得分:5)

你可以这样做:

using IOPath = System.IO.Path;

然后在你的代码中:

class Foo
{
   public string Path;

   void Bar(){ IOPath.Combine("",""); } // compile error here
}

答案 1 :(得分:3)

分开您的推荐信息:

this.Path = System.IO.Path.PathFunction();

我强烈建议在该类中的任何位置使用Path时暗示System.IO命名空间,因为很难区分它们。使用这个。限定符和完整命名空间使它们不同。

相关问题