6

在我的 MVC3 应用程序中,我想创建一个匿名集合,其字段名称如下:

new 
{                                                                            
   Buyer.Firstname = "Jim",
   Buyer.Lastname = "Carrey",
   Phone = "403-222-6487",
   PhoneExtension = "",
   SmsNumber = "",
   Buyer.Company = "Company 10025",
   Buyer.ZipCode = "90210",
   Buyer.City = "Beverly Hills",
   Buyer.State = "CA",
   Buyer.Address1 = "Address 10025"
   Licenses[0].IsDeleted = "False",
   Licenses[0].ID = "6",
   Licenses[0].AdmissionDate = "2,1999",
   Licenses[0].AdmissionDate_monthSelected = "2",                                                                           

                                                                    } 

我想要这个以便在我的应用程序的集成测试期间发送自定义发布请求。如何使用此字段名称声明一个匿名集合?

4

3 回答 3

9

使用匿名对象的匿名集合,如下所示:

Licenses = new [] {
    new {
        IsDeleted = "False",
        ID = "6",
        AdmissionDate = "2,1999",
        AdmissionDate_monthSelected = "2"
    } //, ... and so on
}

......在上下文中:([编辑]哦,我没有看到你的买家......)

new 
{    
    Buyer = new {
        Firstname = "Jim",
        Lastname = "Carrey",
        Company = "Company 10025",
        ZipCode = "90210",
        City = "Beverly Hills",
        State = "CA",
        Address1 = "Address 10025",
    },                                                                        
    Phone = "403-222-6487",
    PhoneExtension = "",
    SmsNumber = "",
    Licenses = new [] {
        new {
            IsDeleted = "False",
            ID = "6",
            AdmissionDate = "2,1999",
            AdmissionDate_monthSelected = "2"
        }
    }
}
于 2012-10-01T13:20:24.100 回答
6

您可以使用对象和集合初始化语法:

var anonymousObject = new 
{                                                                            
    Phone = "403-222-6487",
    PhoneExtension = "",
    SmsNumber = "",
    Buyer = new 
    {
        Firstname = "Jim",
        Lastname = "Carrey",
        Company = "Company 10025",
        ZipCode = "90210",
        City = "Beverly Hills",
        State = "CA",
        Address1 = "Address 10025"
    },
    Licenses = new[]
    {
        new
        {
            IsDeleted = "False",
            ID = "6",
            AdmissionDate = "2,1999",
            AdmissionDate_monthSelected = "2",                                                                           
        }
    }
} 
于 2012-10-01T13:23:01.340 回答
2

试试这个:

var x = new {
    Phone = "403-222-6487",
    PhoneExtension = "",
    SmsNumber = "",
    Buyer = new {
        Firstname = "Jim",
        Lastname = "Carrey",
        Company = "Company 10025",
        ZipCode = "90210",
        City = "Beverly Hills",
        State = "CA",
        Address1 = "Address 10025"
    },
    Licenses = new[] {
        new {
            IsDeleted = "False",
            ID = "6",
            AdmissionDate = "2,1999",
            AdmissionDate_monthSelected = "2"},
        new {
            IsDeleted = "True",
            ID = "7",
            AdmissionDate = "17,2001",
            AdmissionDate_monthSelected = "3"}
    }
};

注意:我为买家使用嵌套匿名类型,为许可证使用另一种匿名类型的嵌套数组。这允许您访问这样的值

string name = x.Buyer.Lastname;
string id = x.Licences[0].ID;
于 2012-10-01T13:26:39.600 回答