【问题标题】:How do I retrieve a Dictionary data type in a web form?如何在 Web 表单中检索 Dictionary 数据类型?
【发布时间】:2013-10-20 05:12:22
【问题描述】:

我有一个返回 Dictionary 对象的 WCF 服务。我创建了一个 ASP .NET Web 应用程序并添加了一个 Web 表单来测试该服务。我在 Web 应用程序中添加了我的 WCF 服务引用。现在,在以 Web 形式编写 Button1_Click 的代码时,我无法访问我的服务返回的 Dictionary 对象。代码如下所示: 请尽快提出解决方案。 谢谢。

`using System;    
using System.Collections.Generic;    
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using WebApplication1.wsHashOps;  

namespace WebApplication1  
{   
    public partial class WebForm1 : System.Web.UI.Page   
    {   
        protected void Page_Load(object sender, EventArgs e)   
        {

      }

        protected void Button1_Click(object sender, EventArgs e)  
        {
            string input = TextBox1.Text;
            Service1 client = new Service1();
            string data = "";



            Dictionary<int, string> hh = client.getWsHashOperations(input);

            string input = TextBox1.Text;    
            Service1 client = new Service1();    
            string data = "";    
            Dictionary<int, string> hh = client.getWsHashOperations(input);        
}        
}        
} 

【问题讨论】:

  • 无法将类型 'WebApplication1.wsHashOps.ArrayOfKeyValueOfintstringKeyValueOfintstring[]' 隐式转换为 'Systems.Collection.Generic.Dictionary'

标签: c# wcf


【解决方案1】:
    KeyValuePair<int,string>[] hh = client.getWsHashOperations(input);

    string input = TextBox1.Text;    
    Service1 client = new Service1();    
    string data = "";    
    KeyValuePair<int,string>[] hh2 = client.getWsHashOperations(input);

您不能声明两个具有相同名称的变量。重命名其中之一。 此外,看起来它会根据您的错误消息解析为 KeyValuePair 数组。试试上面的

在这种情况下也可以使用 var 关键字

var hh = client.getWsHashOperations(input); 

这将为您隐式解析数据类型,同时仍保持类型安全

【讨论】:

  • 抱歉,输入错误。只有一个 hh 对象。我也尝试使用 KeyValuePair 而不是 Dictionary 但仍然显示相同的错误。
  • 好的,可以发一下wcf方法吗?
  • 是的,我知道我可以使用 var,但是如何从 var 数据类型中提取每个字典值?
  • 这里不能发,太长了。但我可以告诉你 wcf 方法的返回类型是 Dictionary.
  • 好的,我相信这可能是由于 wcf 在序列化字典时遇到了一些困难。看看这个stackoverflow.com/questions/1510185/…
猜你喜欢
  • 2017-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多