2016-04-12 40 views
10

因此,今天我在嘗試構建公司解決方案時遇到了有趣的問題,我想問問你們,你們知道爲什麼會發生這種情況。我被告知可能來自我的機器/視覺工作室,因爲其他人沒有同樣的問題。成員訪問調用不會編譯,但靜態調用不會

因此,我們必須在項目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>()public static class 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 : ""; 
} 

我必須指出o我們在項目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

請告訴我發生的事情是編譯器認爲我實際上利用項目BGetAttribute方法。爲什麼發生這種情況?因爲當我試圖導航到GetAttribute VS導致我正確的方法(在ReflectionHelpers)。 難道是由於反射? 注意:我通過靜態調用方法或在我的項目A中添加對System.Xml.Linq的引用來解決此問題,但我很好奇VS /語法檢查功能的奇怪行爲。

+0

你引用包含的XElement組裝?因爲它告訴你_thats_問題;沒有提到方法名稱。我的意思是你嘗試添加System.xml.linq? –

+0

是的,我試過這個問題確實解決了問題,但正如我所說我們找到了解決方案,但奇怪的行爲仍然存在,我很好奇。再加上引用'System.XML.LINQ'就能解決這個問題,但奇怪的是編譯器在引起混淆之前很明顯(對他來說可能不那麼明顯),我沒有使用任何XElements。 – kuskmen

+0

但是你在項目B中做? –

回答

0

我想這是在進行:
- B引用System.Xml.Linq
- B構建沒有問題。
- 你引用A中的B
- 一個還沒有到System.Xml.Linq的
參考 - 一個似乎要消耗B中
定義的功能 - 當您嘗試建立項目A,它產生那個錯誤

對嗎?

如果是這樣的話,這是完全正常的。因爲消耗引用(A)的項目必須引用(B)引用的內容(System.Xml.Linq)。

想這樣:當你嘗試添加nuget包到你的項目,如果它有一個依賴項,nuget也會安裝它。爲什麼?由於這種情況。

如果我正確理解你的答案,這是完全正常的。

+0

問題是爲什麼當Visual Studio的導航清楚地知道哪一個是正確的方法(項目A中的一個)時,編譯器會對項目B中的函數感到困惑?當你回答這個答案時,你會發現原答案的答案,至少我是這麼回答的。看看我的問題下面的評論中的鏈接,它幾乎回答了我所問及的所有問題。 – kuskmen

+0

哈哈!對不起我的英語不好(對於我的回答和我的困惑)。你有權這樣做:-) – zokkan

+0

順便說一下,你正在使用哪個版本的vs? – zokkan

1

這是一個猜測,但我認爲你的函數:

public static TAttribute GetAttribute<TAttribute>(this Type type) where TAttribute : Attribute;一個希望將TAttribute返回類型:

private static string RpcRoutingKeyNamingConvention(Type messageType, ITypeNameSerializer typeNameSerializer)因爲你嘗試返回一個字符串不匹配您的helper方法簽名。

也許,你可以嘗試修改你的函數RpcRoutingKeyNamingConvention返回GlobalRPCRequest並檢查編譯器是否繼續瘋狂。

+0

'不匹配你的幫助方法簽名,因爲你嘗試返回一個字符串:'你是什麼意思它不匹配我的幫助方法,因爲我試圖返回字符串..這是狀態離譜? – kuskmen

+0

@Yann REANAUDIN他不是想要返回一個字符串,看看'? queueName:queueName +「_」+ AvailabilityZone;' – Tokk

1

Visual Studio會一直困惑!我試圖在我的VS 2015(.NET 4.6)中重現場景,它編譯得很好。我沒有必要在我的項目A中添加對System.Xml.Linq的引用。

我的猜測是它可能是緩存問題。你可能想試試這個:

  1. 刪除參照項目B
  2. 清潔然後重建這兩種解決方案
  3. 添加參考後面
  4. 重建,瞧!嗯..希望

希望它能幫助,讓我知道:)