转换ExpandoObject匿名类型 [英] Convert ExpandoObject to anonymous type

查看:682
本文介绍了转换ExpandoObject匿名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能转换 ExpandoObject 匿名类型的对象?

Is it possible to convert ExpandoObject to anonymously typed object?

目前我有的HtmlHelper 扩展属性作为参数。问题是,我的扩展也需要添加一些HTML属性,所以我已经用ExpandoObject合并我的属性和属性用户通过使用htmlAttributes参数的功能。现在,我需要通过合并HTML属性原来的HtmlHelper功能,当我送ExpandoObject,没有任何反应。所以我想,我需要ExpandoObject转换为匿名类型的对象或类似的东西 - 任何建议,欢迎

Currently I have HtmlHelper extension that can take HTML attributes as a parameter. The problem is that my extension also needs to add some HTML attributes so I've use ExpandoObject to merge my attributes and attributes that user passes to the function using htmlAttributes parameter. Now I need to pass merged HTML attributes to original HtmlHelper function, and when I send ExpandoObject, nothing happens. So I guess that I need to convert ExpandoObject to anonymously typed object or something similar - any suggestions are welcome.

推荐答案

我不认为你需要处理expandos的实现自己的目标:

I don't think that you need to deal with expandos to achieve your goal:

public static class HtmlExtensions
{
    public static IHtmlString MyHelper(this HtmlHelper htmlHelper, object htmlAttributes)
    {
        var builder = new TagBuilder("div");

        // define the custom attributes. Of course this dictionary
        // could be dynamically built at runtime instead of statically
        // initialized as in my example:
        builder.MergeAttribute("data-myattribute1", "value1");
        builder.MergeAttribute("data-myattribute2", "value2");

        // now merge them with the user attributes 
        // (pass "true" if you want to overwrite existing attributes):
        builder.MergeAttributes(new RouteValueDictionary(htmlAttributes), false);

        builder.SetInnerText("hello world");

        return new HtmlString(builder.ToString());
    }
}

如果你想打电话现有的一些助手,那么一个简单的foreach循环可以做的工作:

and if you wanted to call some of the existing helpers, then a simple foreach loop could do the job:

public static class HtmlExtensions
{
    public static IHtmlString MyHelper(this HtmlHelper htmlHelper, object htmlAttributes)
    {
        // define the custom attributes. Of course this dictionary
        // could be dynamically built at runtime instead of statically
        // initialized as in my example:
        var myAttributes = new Dictionary<string, object>
        {
            { "data-myattribute1", "value1" },
            { "data-myattribute2", "value2" }
        };

        var attributes = new RouteValueDictionary(htmlAttributes);
        // now merge them with the user attributes
        foreach (var item in attributes)
        {
            // remove this test if you want to overwrite existing keys
            if (!myAttributes.ContainsKey(item.Key))
            {
                myAttributes[item.Key] = item.Value;
            }
        }
        return htmlHelper.ActionLink("click me", "someaction", null, myAttributes);
    }
}

这篇关于转换ExpandoObject匿名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆