2012-04-28 69 views
0

我加入作爲參考3 dll的:Google.Apis,Google.Apis.Translate.v2,System.Runtime.Serialization我得到異常類型加載什麼樣的異常?

在Form1我有一個行:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 


     public Form1() 
     { 
      InitializeComponent(); 


      Translator.translate(new TranslateInput()); 

     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 
    } 
} 

現在錯誤的例外是在類翻譯的第一行: 拋出錯誤的代碼行是:var service = new TranslateService { Key = GetApiKey() };

類代碼是:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Net; 
using System.IO; 
using System.Web; 
using System.Runtime.Serialization; 
using System.Runtime.Serialization.Json; 
using Google.Apis.Util; 
using Google.Apis.Translate.v2; 
using Google.Apis.Translate.v2.Data; 
using TranslationsResource = Google.Apis.Translate.v2.Data.TranslationsResource; 


public class Translator 
{ 
    public static string translate(TranslateInput input) 
    { 
     // Create the service. 
     var service = new TranslateService { Key = GetApiKey() }; 
     string translationResult = ""; 

// Execute the first translation request. 
     Console.WriteLine("Translating to '" + input.TargetLanguage + "' ..."); 
     TranslationsListResponse response = service.Translations.List(input.SourceText, input.TargetLanguage).Fetch(); 
     var translations = new List<string>(); 
     foreach (TranslationsResource translation in response.Translations) 
     { 
      translationResult = translation.TranslatedText; 
     } 
     return translationResult; 
    } 
    private static string GetApiKey() 
    { 
     return "AIzaSyCjxMe6RKHZzd7xSfSh2pEsBqUdXYm5tA8"; // Enter Your Key 
    } 
} 

/// <summary> 
/// User input for this example. 
/// </summary> 
[Description("input")] 
public class TranslateInput 
{ 
    [Description("text to translate")] 
    public string SourceText = "Who ate my candy?"; 
    [Description("target language")] 
    public string TargetLanguage = "fr"; 
} 

錯誤是:

無法從程序集'Google.Apis,版本= 1.1.4497.35846,Culture = neutral,PublicKeyToken = null'加載類型'Google.Apis.Discovery.FactoryParameterV1_0'。

試圖谷歌的幫助,也試圖改變項目類型爲x64平臺,但它沒有幫助。所以我把它放回到x86

我有Windows 7 64位Visual Studio C#2010親.net 4.0配置文件客戶端。

不知道錯誤是什麼?

回答

2

上面發佈的消息中報告的錯誤是由於解決方案或項目的bin \ Debug文件夾中存在本地副本。即使您嘗試清理解決方案,這些副本仍會存在。

爲了避免發生這種情況,您必須強制Visual Studio通過在項目屬性中添加引用路徑來引用正確的DLL。不幸的是,如果你的解決方案中有多個項目,你將不得不一個接一個地設置項目的參考路徑,直到完成。

如果您想知道如何設置參考路徑遵循這些簡單的說明:

1.Select您的項目,單擊鼠標右鍵,然後單擊「屬性」; 2.在項目屬性中,單擊「參考路徑」; 3.文件夾,輸入或瀏覽到您的DLL的正確位置,單擊[添加文件夾]。

您將需要執行這些步驟以獲取您可能擁有的每個DLL的不同位置。考慮在相同項目屬性的Build選項卡下設置一個輸出路徑,以便您可以將DLL輸出到同一個目錄中,從而確保您可以在同一位置查找所有最新版本,從而簡化您的引用。

請注意,這隻能是此錯誤的一個原因。但可以肯定的是,必須對所提到的程序集的錯誤副本進行一些操作。

相關問題