当未引用库时,Visual Studio选择错误的构造函数吗? [英] Visual Studio chooses wrong constructor when a library isn't referenced?

查看:118
本文介绍了当未引用库时,Visual Studio选择错误的构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Visual Studio 2010用于两个项目.

I am using Visual Studio 2010 with two projects.

一个包含一个引用Microsoft的Exchange.WebServices dll(ver1.2)的项目来访问ExchangeServices.我创建了一个包含一些辅助方法和包装器的类,以在连接到Exchange服务器时(通过ExchangeService API)执行各种任务. ExchangeService构造函数可以接受ExchangeVersion的枚举,以指定服务器版本信息.所以我在类中创建了两个构造函数.

One contains a project referencing Microsoft's Exchange.WebServices dll(ver1.2) for accessing ExchangeServices. I created a class which contains some helper methods and wrappers to carry out various tasks while connected to an Exchange server(through the ExchangeService API). The ExchangeService constructor can accept an enum of ExchangeVersion, to specify the server version info. So I created two constructors within my class.

public class ExchangeConnector(string ver)
{
    // Property assignments
}

public class ExchangeConnector(ExchangeVersion ver)
    :this(ver.toString()) //Using(or not using) "this", doesn't seem to matter...
{ }

我创建了一个接受字符串参数的构造函数,因此其他项目不必添加Exchange.WebServices库.

I created the constructor which accepts a string parameter, so that other projects don't necessarily need to add the Exchange.WebServices library.

但是后来我遇到了一个无法预料的问题.

But then I ran into an un-foreseen issue.

当我在第二个项目(不包含对Exchange.WebServices dll的引用)中创建ExchangeConnector("Exchange2007_SP1")实例时,Intellisense不会选择正确的构造函数,并且不会显示任何预编译错误.但是,当我强制构建时,出现以下错误:

When I create an instance of ExchangeConnector("Exchange2007_SP1") in my second project(that does not contain a reference to the Exchange.WebServices dll), Intellisense doesn't pick the right constructor and doesn't show any pre-compile errors. When I force the build, though, I get the following error:

Error: The type 'Microsoft.Exchange.WebServices.Data.ExchangeVersion' is defined
in an assembly that is not referenced. You must add a reference to assembly
'Microsoft.Exchange.WebServices, Version=14.0.0.0, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35'.

我什至没有将构造函数与ExchangeVersion枚举引用一起使用,但是它需要我对其进行引用吗?

I am not even using the constructor with the ExchangeVersion enum reference, but it requires me to have a reference to it?

如果我用ExchangeVersion枚举注释掉构造函数,则所有内容均可编译,运行,并且不会出现运行时错误. 或者 如果我修改了重载构造函数,则Intellisense不可能将两者混淆,例如:

If I comment out the constructor with the ExchangeVersion enum, everything compiles, works, no run-time errors. OR If I modify the overload constructor so Intellisense can't possibly confuse the two, such as:

public class ExchangeConnector(string url, ExchangeVersion ver)
{ 
  // Property assignments
}

当我调用ExchangeConnector("Exchange2007_SP1")时,代码将编译并正常工作.没有运行时错误.

When I call ExchangeConnector("Exchange2007_SP1"), the code compiles and works fine. No runtime errors.

几乎就像VS无法解析要正确使用的构造函数.现在,我知道可以将引用添加到第二个项目并完成它,但是我很好奇为什么VS会这样做.有什么想法吗?

It is almost as though VS can't resolve which constructor to properly use. Now I know I can add the reference to the second project and be done with it, but I am curious as to why VS is doing this. Any ideas?

推荐答案

在从Scott(另一个)的启发和CodeCaster提供的链接之后,我想我终于找到了答案.

After inspiration from Scott(the other one) and the link provided by CodeCaster, I think I finally found my answer.

它是C#语言规范(Visual Studio .Net 2003版)的一部分.

It as part of the C# Language Specification(Visual Studio .Net 2003 Edition).

第10.10节实例构造器中:

形式为base(argument-listopt)的实例构造函数初始化器导致直接基类中的实例构造函数被调用.使用参数列表和第7.4.2节的重载解析规则选择该构造函数.候选实例构造函数集由直接基类中包含的所有可访问实例构造函数(包括第10.10.4节中定义的任何默认构造函数)组成.如果此集合为空,或者无法识别单个最佳实例构造函数,则会发生编译时错误.

An instance constructor initializer of the form base(argument-listopt) causes an instance constructor from the direct base class to be invoked. That constructor is selected using argument-list and the overload resolution rules of Section 7.4.2. The set of candidate instance constructors consists of all accessible instance constructors contained in the direct base class (including any default constructor, as defined in Section 10.10.4). If this set is empty, or if a single best instance constructor cannot be identified, a compile-time error occurs.

深入研究定义...

第7.4.2节超载分辨率"中:

一旦确定了候选函数成员和参数列表,则在所有情况下最佳函数成员的选择都是相同的:

Once the candidate function members and the argument list have been identified, the selection of the best function member is the same in all cases:

  • 给定一组适用的候选功能成员,找到该集合中最佳功能成员.
  • 如果集合中仅包含一个功能成员,则该功能成员是最佳功能成员.
  • 否则,相对于给定参数列表,最佳函数成员是一个比所有其他函数成员都要好的函数成员,条件是使用7.4.2.2节中的规则将每个函数成员与所有其他函数成员进行比较
  • 如果没有一个函数成员比所有其他函数成员都要好,那么该函数成员的调用将是模棱两可的,并且会发生编译时错误.
  • Given the set of applicable candidate function members, the best function member in that set is located.
  • If the set contains only one function member, then that function member is the best function member.
  • Otherwise, the best function member is the one function member that is better than all other function members with respect to the given argument list, provided that each function member is compared to all other function members using the rules in Section 7.4.2.2.
  • If there is not exactly one function member that is better than all other function members, then the function member invocation is ambiguous and a compile-time error occurs.

甚至更多...

第7.4.2.2节,更好的功能成员[3]

Section 7.4.2.2 Better function member[3]

[3] [http://msdn.microsoft.com/zh-cn/library/aa691338(v = vs.71).aspx]

[3][http://msdn.microsoft.com/en-us/library/aa691338(v=vs.71).aspx]

给出一个具有一组参数类型{A1,A2,...,AN}的参数列表A以及两个具有参数类型{P1,P2,...,PN}和{Q1的适用函数成员MP和MQ ,Q2,...,QN},如果满足以下条件,则MP被定义为比MQ更好的函数成员:

Given an argument list A with a set of argument types {A1, A2, ..., AN} and two applicable function members MP and MQ with parameter types {P1, P2, ..., PN} and {Q1, Q2, ..., QN}, MP is defined to be a better function member than MQ if

  • 对于每个参数,从AX到PX的隐式转换并不比从AX到QX的隐式转换差,并且
  • 至少有一个参数,从AX到PX的转换要好于从AX到QX的转换.

执行此评估时,如果MP或MQ以扩展形式适用,则PX或QX以参数列表的扩展形式引用参数.

When performing this evaluation, if MP or MQ is applicable in its expanded form, then PX or QX refers to a parameter in the expanded form of the parameter list.

总而言之:

具有相同数量参数的构造函数必须通过隐式转换来评估,以确定哪个函数member(constructor)更好.

A constructor with the same number of arguments must be evaluated through implicit conversion as to which function member(constructor) would be better.

因此,为什么我需要将引用添加到第二个项目,以便编译器可以确定哪个构造函数更好.

Hence why I needed to add the reference to the second project so the compiler could determine which constructor was better.

如果构造函数没有相同数量的参数,则不需要进行求值,并且编译器不需要引用第二个项目.

If the constructors don't have the same number of arguments, evaluation doesn't need to occur, and the compiler doesn't need the reference to the second project.

这篇关于当未引用库时,Visual Studio选择错误的构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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