加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 06:43:57 所属栏目:百科 来源:网络整理
导读:所以今天在尝试构建我们公司的解决方案时遇到了一些有趣的问题,我想问你们,你们知道为什么会发生这种情况.我被告知可能来自我的机器/视觉工作室,因为其他人没有相同的问题. 所以我们在项目A中有一个方法: private static string RpcRoutingKeyNamingConvent
所以今天在尝试构建我们公司的解决方案时遇到了一些有趣的问题,我想问你们,你们知道为什么会发生这种情况.我被告知可能来自我的机器/视觉工作室,因为其他人没有相同的问题.

所以我们在项目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:RepositorieswebsitewebsitesubmodulescoresrcAExtensionsExtensions.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并检查编译器是否继续疯狂.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读