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)
        {

        }
    }
}

现在异常错误出现在 Translator 类的第一行: 引发错误的行是: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,文化=中性,PublicKeyToken=null”加载类型“Google.Apis.Discovery.FactoryParameterV1_0”。

试图谷歌寻求帮助,还试图将项目类型更改为 x64 平台,但没有帮助。所以我把它放回x86

我有 windows 7 64bit visual studio c# 2010 pro .net 4.0 配置文件客户端。

无法弄清楚错误是什么?

4

1 回答 1

2

上述消息中报告的此错误是由于解决方案或项目的 bin\Debug 文件夹中的本地副本造成的。即使您尝试清理您的解决方案,此类副本仍将继续存在。

为了避免这种情况发生,您必须通过在项目属性中添加引用路径来强制 Visual Studio 引用正确的 DLL。不幸的是,如果您的解决方案中有多个项目,则必须一个接一个地设置项目的参考路径,直到完成。

如果您想知道如何设置参考路径,请遵循以下简单说明:

1.选择你的项目,右击,然后点击“属性”;2.在项目属性中,点击“参考路径”;3.文件夹,键入或浏览到您的DLL的正确位置,单击[添加文件夹]。

您将需要针对每个 DLL 可能拥有的许多不同位置执行这些步骤。考虑在相同项目属性的 Build 选项卡下设置输出路径,以便您可以将每个 DLL 输出到同一目录中,从而确保您在同一位置找到所有最新构建,从而简化您的引用。

请注意,这只能是此错误的一个原因。但可以肯定的是,这与上述程序集的错误副本有关。

于 2012-04-28T22:14:00.777 回答