Php 2d数组为C#2d数组/结构

时间:2010-04-23 12:17:33

标签: c# php arrays struct

我正在使用MailChimp的API订阅电子邮件到列表。函数listsubscribe()用于电子邮件订阅:

public static  listSubscribe(string apikey, string id, string email_address, array merge_vars, string email_type, boolean double_optin, boolean update_existing, boolean replace_interests, boolean send_welcome)

我下载了MailChimp官方.NET wrapper的API

查看Visual Studio时,这是一个重载函数:

listSubscribe(string apikey, string id, string email_address, MCMergeVar[] merges)

当我点击MCMergeVar []的定义时,会出现:

[XmlRpcMissingMapping(MappingAction.Ignore)]
    public struct MCMergeVar
    {
        public string name;
        public bool req;
        [XmlRpcMissingMapping(MappingAction.Error)]
        public string tag;
        public string val;
    }

在MailChimp网站的php示例中,这是merges变量声明的方式:

$merge_vars = array('FNAME'=>'Test', 'LNAME'=>'Account', 'INTERESTS'=>'');

如何为我的C#包装器正确编写此数组? 我试过这样的事情:

MCMergeVar[] subMergeVars = new MCMergeVar[1];
subMergeVars["FNAME"] = "Test User";

但它需要int,而"FNAME"现已放置,所以这不起作用......

提前致谢, ILE

编辑1: 我尝试了FoxFire的解决方案,但没有来自subMergeVars的数据传递给MailChimp服务器,只传递了电子邮件:

// Subscribe email to list
            string subID = "26973e52cc";
            string subEmail = "mymail@some.com.hr";
            MCMergeVar[] subMergeVars = new MCMergeVar[5];
            subMergeVars[0].name = "FNAME";
            subMergeVars[0].val = "FNDynamic";
            subMergeVars[1].name = "LNAME";
            subMergeVars[1].val = "LNDynamic";

            mailChimp.api.listSubscribe(subID, subEmail, subMergeVars, "html");

2 个答案:

答案 0 :(得分:1)

最有可能:

MCMergeVar[] subMergeVars = new MCMergeVar[1]; 
subMergeVars[0].name = "FNAME"; 
subMergeVars[0].val = "Test User"; 

答案 1 :(得分:0)

尝试:

var mergeVars = new List<MCMergeVar>();

mergeVars.Add(new MCMergeVar() { tag = "FNAME", val = "Test User First Name" });
mergeVars.Add(new MCMergeVar() { tag = "LNAME", val = "Test User Last Name" });

然后使用:

mergeVars.ToArray()