【发布时间】:2016-10-19 13:24:33
【问题描述】:
我在我的扩展方法解析中看到了一些意外行为,我正试图找出原因。我已经阅读了Eric Lippert's Closer is Better 的文章,但我仍然感到困惑。
首先调用站点:
// Assembly: DLaB.Xrm.2016.Tests
using DLaB.Xrm.Entities;
using DLaB.Xrm.Test;
using Microsoft.Xrm.Sdk;
namespace DLaB.Xrm.Tests
public class TrialClass{
{
public void Delete(){
IOrganziationService service = GetService();
Id<Contact> id = GetId();
service.Delete(id); // Call Site in Question
}
}
}
IOrganizationService 使用此签名定义删除方法:
// Assembly: Microsoft.Xrm.Sdk
void Delete(string entityName, Guid id);
我在两个不同的命名空间、两个不同的程序集中定义了 IOrganziationService 的扩展方法重载:
//Assembly: DLaB.Xrm.2016
//Namespace DLaB.Xrm
public static void Delete(this IOrganizationService service, Entity entity) { ... }
public static void Delete(this IOrganizationService service, EntityReference entity) { ... }
//Assembly: DLaB.Xrm.Test.2016
//Namespace DLaB.Xrm.Test
public static void Delete(this IOrganizationService service, Id id) { ... }
public static void Delete<T>(this IOrganizationService service, Id<T> entity) where T : Entity { ... }
我还为 Id 类定义了隐式运算符,将其转换为 Entity 或 EntityReference
调用站点
service.Delete(id)显示编译器错误: 错误 CS0121 调用在以下方法之间不明确或 属性:'Extensions.Delete(IOrganizationService,Entity)'和 'Extensions.Delete(IOrganizationService, EntityReference)'
我从我定义的隐式运算符中了解到这一点。我不明白的是,为什么 DLaB.Xrm 命名空间比 DLaB.Xrm.Test 命名空间更接近,即使我没有 using 指令。
【问题讨论】:
标签: c# extension-methods overload-resolution