奇怪"装配不引用"试图调用一个有效的方法重载时出错 [英] Weird "assembly not referenced" error when trying to call a valid method overload

查看:201
本文介绍了奇怪"装配不引用"试图调用一个有效的方法重载时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用方法重载的大会A

public static int GetPersonId(EntityDataContext context, string name)
{
    var id = from ... in context... where ... select ...;
    return id.First(); 
}

public static int GetPersonId(SqlConnection connection, string name)
{
    using (var context = new EntityDataContext(connection, false))
    {
        return GetPersonId(context, name);
    }
}

当我尝试从组件B 拨打第二个重载,VS生成以下编译时错误:

When I try to call the second overload from Assembly B, VS produces the following compile-time error:

类型System.Data.Entity.DbContext是一个集定义   未被引用。您必须添加一个引用组件   的EntityFramework,版本= 6.0.0.0,文化=中立,   公钥= ...'。

The type 'System.Data.Entity.DbContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=...'.

组件B 引用大会A 。实体框架中引用仅在大会A 组件B 不使用它。从组件B 调用看起来是这样的:

Assembly B references Assembly A. Entity Framework is referenced only in Assembly A as Assembly B doesn't use it. The call from Assembly B looks like this:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();                               
    var id = AssemblyA.GetPersonId(connection, name); // compiler error
    ...
}  

我不明白的是,错误消失,如果我改变方法的签名大会A 来,例如:

public static int GetPersonId(SqlConnection connection, string name, bool empty)

和改变调用:

var id = AssemblyA.GetPersonId(connection, name, true); // no error

为什么我的code编译没有在第一种情况?这似乎并不可能与实体框架的错误指示。我一直认为C#允许方法重载,其中方法签名的参数类型的唯一区别。例如。我可以运行以下code没有如预期的问题:

Why is my code not compiling in the first case? It doesn't seem to be related to Entity Framework as the error indicates. I have always thought that C# allows method overloading where method signatures differ only in parameter types. E.g. I can run the following code without problems as expected:

static void DoStuff(int a, int b) { ... }
static void DoStuff(int a, float b) { ... }

DoStuff(10, 5);
DoStuff(10, 5.0f);

那么,为什么我会得到我的情况下错误,尽管表面上合法的重载?

So why am I getting the error in my situation despite the apparently legal overloads?

请注意,从组件B 我没有问题要求在内部使用 EntityDataContext 等方法,唯一不同的是这些方法不必重载

Note that from Assembly B I have no problems calling other methods that internally use EntityDataContext, the only difference is that these methods don't have overloads.

背景

EntityDataContext 继承了EF英孚的的DbContext

public partial class EntityDataContext : DbContext
{
        public EntityDataContext() : base("name=EntityDataContext") { }

        public EntityDataContext(DbConnection connection, bool contextOwnsConnection) 
            : base(connection, contextOwnsConnection) { }

        ...
}

我使用.NET 4.0与EF 6 code率先添加了一些自定义构造函数重载现有的数据库。

I'm using .NET 4.0 with EF 6 code first to an existing database with some custom ctor overloads added.

推荐答案

C#的标准规定,重载解析(第7.5.3节)通过比较每个匹配的签名,以确定哪些是更适合执行。这并不是说当参考缺少会发生什么,所以我们必须推断,它仍然需要比较那些未引用类型。

The C# standard specifies that overload resolution (section 7.5.3) is performed by comparing each matching signature to determine which is a better fit. It doesn't say what happens when a reference is missing, so we must infer that it still needs to compare those unreferenced types.

在你的情况,编译器需要引用 EntityDataContext 才能够比较两个重载。您的通话签名完全匹配因此从理论上讲,你不应该需要这个,但是标准没有规定任何这样的短路现象。

In your case, the compiler needs a reference to EntityDataContext to be able to compare the two overloads. Your call matches the signature exactly so in theory you shouldn't need this, but the standard doesn't specify any such short-circuit behavior.

这篇关于奇怪"装配不引用"试图调用一个有效的方法重载时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆