C#相当于php关联数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9703937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# equivalent to php associative array
提问by Ullas Prabhakar
I'm customizing a shopping cart application in php. In this application, I have to integrate some parts with another C#.net application, so I'm using a webservice in a php shopping cart. In one method of the webservice, some values should pass as an associative array like this:
我正在用 php 自定义购物车应用程序。在这个应用程序中,我必须将某些部分与另一个 C#.net 应用程序集成,所以我在 php 购物车中使用了一个 web 服务。在 webservice 的一种方法中,一些值应该作为关联数组传递,如下所示:
$proxy = new SoapClient('www.mywebservice.com?wsdl');
$associative_array= array(
'abc'=> 1,'def'=>0,'ghi'=>1,'jkl'=>0
);
$proxy->call($sessionId, 'methodname', array('somevalue', $associative_array));
In php its working fine... but the problem is that I'm struggling with C#.net, how can I pass an associative array with C#.net? I'm a php programmer, I think there's no associative array in C#.net and somebody said that C# Dictionary can be used instead of that, but that's not working with the webservice call.
在 php 中它工作正常......但问题是我在 C#.net 上挣扎,我如何通过 C#.net 传递关联数组?我是一名 php 程序员,我认为 C#.net 中没有关联数组,有人说可以使用 C# Dictionary 代替它,但这不适用于 webservice 调用。
C# code is:
C#代码是:
Dictionary<string,string> map=new Dictionary<string,string>();
map.Add("abc","1");
map.Add("def","0");
object st = mgs.call(sessionid, "methodname", new object[] { "somevalue",map });
Can anybody give some advice???
谁能给点建议???
回答by Daniel A. White
I think you want a Dictionary<string, int>. But I could be wrong. You should see what the generated class is used when you call the web service.
我想你想要一个Dictionary<string, int>. 但我可能是错的。您应该看到在调用 Web 服务时使用了生成的类。
To call the web service, right click the References folder of your project. Say Add Service Reference.
要调用 Web 服务,请右键单击项目的 References 文件夹。说添加服务引用。
Put the WSDL url in there and let it generate the classes for you.
将 WSDL url 放在那里,让它为您生成类。
回答by Daniel A. White
回答by Ken
My guess is that the reason the Dictionary isn't working with the web service call is that Dictionary isn't serializable. Try a SerializableDictionary instead; there's an implementation here:
我的猜测是 Dictionary 不与 Web 服务调用一起工作的原因是 Dictionary 不可序列化。尝试使用 SerializableDictionary 代替;这里有一个实现:
http://www.dacris.com/blog/2010/07/31/c-serializable-dictionary-a-working-example/
http://www.dacris.com/blog/2010/07/31/c-serializable-dictionary-a-working-example/

