【发布时间】:2021-11-06 05:43:22
【问题描述】:
我将Google.Api.CommonProtos NuGet 包与我的C# 服务一起使用,并希望将Google.Type.Date 类型与System.DateTime 类型一起使用。我还想使用包含的扩展方法在System.DateTime 和Google.Type.Date 之间来回转换。
问题是,当我导入扩展方法.ToDate() 时,它会引入整个Google.Type 命名空间,这会导致DateTime 在System.DateTime 和Google.Type.DateTime 之间产生歧义。此代码未构建:
using System;
using Google.Type;
namespace Example
{
public class ExampleClass
{
public Google.Type.Date TomorrowDate(DateTime date) => date.AddDays(1).ToDate();
}
}
// Error CS0104 'DateTime' is an ambiguous reference between 'Google.Type.DateTime' and 'System.DateTime'
我可以在引用 Google.Type.DateTime 时保持冗长,但我不想开始在我们的应用程序中重写 DateTime 的所有用法来说 System.DateTime。我怎样才能做到这一点?实际上,如何在不导入(C# 中的using)声明扩展方法的整个命名空间的情况下使用特定类中定义的扩展方法?
【问题讨论】:
标签: c# datetime static namespaces extension-methods