Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Member access call does not compile but static call does

So today I faced interesting problem while trying to build our company solution and I wanted to ask you guys do you know why is this happening. I've been told that it might be from my machine/visual studio because other people did not have same problem.

So we have a method in project A:

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

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

where GetAttribute<GlobalRPCRequest>() is defined in public static class ReflectionHelpers

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

then we have project B which have method:

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

I have to point out that we have reference to project B in project A. Now what happens is that when I try to build I get compile error:

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

Whats happening is that compiler thinks that I am actually using GetAttribute method from project B(in my opinion!). Why this is happening? Since when I try to navigate to GetAttribute VS leads me to the right method (the one that is in ReflectionHelpers). Could it be because of the reflection? NOTE: I fixed this issue by calling the method statically or adding reference to System.Xml.Linq in my project A, but I am curious of the strange behavior of VS/syntax-checking feature.

like image 634
kuskmen Avatar asked Apr 12 '16 14:04

kuskmen


2 Answers

It's a guess, but I think your function:

private static string RpcRoutingKeyNamingConvention(Type messageType, ITypeNameSerializer typeNameSerializer) does not match your helper method signature because you try returning a string:

public static TAttribute GetAttribute<TAttribute>(this Type type) where TAttribute : Attribute; which expects a TAttribute return type.

Maybe, you can try modifiying your function RpcRoutingKeyNamingConvention to return GlobalRPCRequest and check if the compiler continues to go crazy.

like image 105
Yann RENAUDIN Avatar answered Oct 17 '22 05:10

Yann RENAUDIN


Visual Studio gets confused all the times! I tried to reproduce the scenario in my VS 2015 (.NET 4.6) and it compiles just fine. I didn't have to add reference to System.Xml.Linq in my Project A.

My guess is that it might be a cache issue. You might want to try this:

  1. Remove reference to Project B
  2. Clean then rebuild both solutions
  3. Add the reference back
  4. Rebuild and voila!! Well.. hopefully

Hope it helps, let me know :)

like image 45
kent-id Avatar answered Oct 17 '22 06:10

kent-id