c# – 成员访问调用不编译但静态调用

所以今天在尝试构建我们公司的解决方案时遇到了一些有趣的问题,我想问你们,你们知道为什么会发生这种情况.我被告知可能来自我的机器/视觉工作室,因为其他人没有相同的问题.

所以我们在项目A中有一个方法

private static string RpcRoutingKeyNamingConvention(Type messageType,ITypeNameSerializer typeNameSerializer)
{
   string queueName = typeNameSerializer.Serialize(messageType);

   return messageType.GetAttribute<GlobalRPCRequest>() != null || AvailabilityZone == null
        ? queueName
        : queueName + "_" + AvailabilityZone;
}

其中GetAttribute< GlobalRPCRequest>()在公共静态类ReflectionHelpers中定义

public static TAttribute GetAttribute<TAttribute>(this Type type) where TAttribute : Attribute;

那么我们有项目B有方法

public static string GetAttribute(this XElement node,string name)
{
   var xa = node.Attribute(name);
   return xa != null ? xa.Value : "";
}

我必须指出,我们参考了项目A中的项目B.
现在发生什么是当我尝试构建我得到编译错误

Error 966 The type ‘System.Xml.Linq.XElement’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Xml.Linq,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089’. D:\Repositories\website\website\submodules\core\src\A\Extensions\Extensions.cs 37 13 A

发生的是编译器认为我实际上是使用项目B中的GetAttribute方法(在我看来!).为什么会发生这种情况?因为当我尝试导航到GetAttribute VS导致我正确的方法(在ReflectionHelpers中的那个).
可能是因为反思吗?注意:我通过在我的项目A中静态调用方法添加对System.Xml.Linq的引用来修复此问题,但我很好奇VS /语法检查功能的奇怪行为.

解决方法

这是一个猜测,但我认为你的功能

私有静态字符串RpcRoutingKeyNamingConvention(类型messageType,ITypeNameSerializer typeNameSerializer)与您的帮助方法签名不匹配,因为您尝试返回一个字符串:

public static TAttribute GetAttribute< TAttribute>(此类型类型)其中TAttribute:Attribute;这需要一个TAttribute返回类型.

也许您可以尝试修改函数RpcRoutingKeyNamingConvention返回GlobalRPCRequest并检查编译器是否继续疯狂.

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么