如何在匿名类型中使用复杂的属性名称?

时间:2012-10-01 13:17:24

标签: c# asp.net-mvc-3 post anonymous-types

在我的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",                                                                           

                                                                    } 

我希望这能够在我的应用程序的集成测试期间发送自定义帖子请求。如何使用此字段名称声明匿名集合?

3 个答案:

答案 0 :(得分:7)

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

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"
        }
    }
}

答案 1 :(得分: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",                                                                           
        }
    }
} 

答案 2 :(得分: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;