C# 类似JSON的动态对象数组

C# 类似JSON的动态对象数组,c#,asp.net-mvc,C#,Asp.net Mvc,我想创建一个动态对象数组,类似于JSON模式。我使用它来允许我的Breadcrumb类接受定义输出特性的参数。我想它看起来会像这样 breadcrumbs.Add(new Breadcrumb() { Title = "Page name", Attributes = { class = "myclass", data-info="info stuff" } }); <a href="" title="Page name" class="myclass" data=

我想创建一个动态对象数组,类似于JSON模式。我使用它来允许我的
Breadcrumb
类接受定义输出特性的参数。我想它看起来会像这样

breadcrumbs.Add(new Breadcrumb() { 
    Title = "Page name", 
    Attributes = { class = "myclass", data-info="info stuff" } 
});
<a href="" title="Page name" class="myclass" data=info="into stuff">...</a>
然后,当我显示breadcrum时,我将遍历这个数组以输出如下内容

breadcrumbs.Add(new Breadcrumb() { 
    Title = "Page name", 
    Attributes = { class = "myclass", data-info="info stuff" } 
});
<a href="" title="Page name" class="myclass" data=info="into stuff">...</a>

问题是,我是否可以动态执行此操作,而无需创建位于两者之间的锅炉板代码,即
BreadcrumAttribute
,它可以指定属性名称,然后指定数据


我在MVC.NET的其他地方看到过类似的模式,但记不起它在哪里…

您可以使用如下匿名类型:

breadcrumbs.Add(new Breadcrumb() { 
    Title = "Page name", 
    Attributes = new { @class = "myclass", datainfo="info stuff" } 
});
data info
不允许作为属性名,因此我将其更改为
datainfo
,您也可以将其命名为
data\u info

或者将其定义为
字典
,并像以下那样使用:

breadcrumbs.Add(new Breadcrumb() { 
    Title = "Page name", 
    Attributes = new Dictionary<string, string>() { { "class", "myclass" }, { "data-info", "info" } }
});
breadcrumbs.Add(新的Breadcrumb(){
Title=“页面名称”,
Attributes=newdictionary(){{“class”,“myclass”},{“data info”,“info”}
});

您可以使用以下匿名类型:

breadcrumbs.Add(new Breadcrumb() { 
    Title = "Page name", 
    Attributes = new { @class = "myclass", datainfo="info stuff" } 
});
data info
不允许作为属性名,因此我将其更改为
datainfo
,您也可以将其命名为
data\u info

或者将其定义为
字典
,并像以下那样使用:

breadcrumbs.Add(new Breadcrumb() { 
    Title = "Page name", 
    Attributes = new Dictionary<string, string>() { { "class", "myclass" }, { "data-info", "info" } }
});
breadcrumbs.Add(新的Breadcrumb(){
Title=“页面名称”,
Attributes=newdictionary(){{“class”,“myclass”},{“data info”,“info”}
});

制作面包屑属性词典

class Breadcrumb {
    public string Title;
    public Dictionary<string,string> Attributes;
}
class面包屑{
公共字符串标题;
公共词典属性;
}
然后

        var breadcrumbs = new List<Breadcrumb>
            {
                new Breadcrumb()
                    {
                        Title = "Page name",
                        Attributes =
                            new Dictionary<string, string>
                                {
                                    {"class", "myclass"}, 
                                    {"data-info", "info stuff"}
                                }
                    }
            };
var breadcrumbs=新列表
{
新面包屑()
{
Title=“页面名称”,
属性=
新词典
{
{“类”,“我的类”},
{“数据信息”,“信息素材”}
}
}
};

制作面包屑属性词典

class Breadcrumb {
    public string Title;
    public Dictionary<string,string> Attributes;
}
class面包屑{
公共字符串标题;
公共词典属性;
}
然后

        var breadcrumbs = new List<Breadcrumb>
            {
                new Breadcrumb()
                    {
                        Title = "Page name",
                        Attributes =
                            new Dictionary<string, string>
                                {
                                    {"class", "myclass"}, 
                                    {"data-info", "info stuff"}
                                }
                    }
            };
var breadcrumbs=新列表
{
新面包屑()
{
Title=“页面名称”,
属性=
新词典
{
{“类”,“我的类”},
{“数据信息”,“信息素材”}
}
}
};

这看起来像是我想要的-属性是什么类型?@Chris他们将是匿名类型,但我如何将其定义为类的一部分?我不能在类定义中使用
var
。@Chris将其定义为
Object
,或者使用字典的第二种解决方案。你也可以使用
dynamic
而不是
Object
这看起来像是我想要的-属性是什么类型?@Chris他们将是匿名类型我如何将其定义为类的一部分?我不能在类定义中使用
var
。@Chris将其定义为
Object
,或者使用字典的第二种解决方案。你也可以使用
dynamic
而不是
Object
你知道,我一直在忙着思考一些聪明的事情,我从来没有想过要把它们简单化。谢谢另一个答案可能更优雅!你知道吗,我忙着想一些聪明的事情,我从来没有想过要简单一点。谢谢另一个答案可能更优雅!