C# 从字符串中的属性创建对象

C# 从字符串中的属性创建对象,c#,json,asp.net-mvc,razor,expandoobject,C#,Json,Asp.net Mvc,Razor,Expandoobject,应创建具有类似json符号字符串的附加属性的对象。 方法将从Razor视图调用,以将colmodel传递给jqgrid 类似于json对象 @Html.Raw( Json.Encode( Model.GetColModel())) 方法应该具有类似签名的属性 object GetColModel(string colName, int colWidth, string additonalProperties) 比如说, GetColModel("customer", 17, "address

应创建具有类似json符号字符串的附加属性的对象。 方法将从Razor视图调用,以将colmodel传递给jqgrid 类似于json对象

@Html.Raw( Json.Encode( Model.GetColModel()))
方法应该具有类似签名的属性

object GetColModel(string colName, int colWidth, string additonalProperties)
比如说,

GetColModel("customer", 17, "address=\"Toronto\", index=1555" )
应该返回对象

new { colName="customer", colwidth=17, address="Toronto", index=1555 }
可能存在类似于JSON、eq中的嵌套属性

GetColModel("customer", 17, "formatoptions= new { formatter=\"number\", editable=true } " )
应该返回对象

new { colName="customer", colwidth=17, formatoptions=new {
                   formatter="number", 
                   editable=true
                   }
}
我试过这个方法

    public object GetColModel(string colName, int colWidth, string additonalProperties)
    {
        return new
        {
            name = colName,
            width = colWidth,
            &addtitionalProperties
        };
    }
但是,由于C语言不支持宏,因此此操作失败#

如何创建这样的方法或以其他方式将属性从数据库添加到Razor视图中的json

它是从ASP.NET/Mono C#MVC 4 viewmodel调用的。
使用Razor视图和RazorEngine。

没有内置的功能,但是可以使用字符串解析字符串(string.Split将允许您在“,”上进行拆分,但如果您的文本中可能包含这些内容,则必须构建一个真正的解析器,或者将字符串格式切换到类似CSV的格式,在那里您可以找到许多解析器。您可能能够找到一个用于简单语法的属性解析器。或者您可以将附加属性字符串作为json并使用json.ne不需要解析

将字符串解析为键/值结构后,可以使用ExpandooObject填充最终对象并返回该对象。

这是一个真正基于json的解决方案的简单实现。
您可以使用以下命令调用它:

dynamic d = Model.GetColModel("customer", 17, " { formatoptions : { formatter : \"number\", editable :true }, stam :2}");
实施:

static class ModelExtension
{   
    public static dynamic GetColModel(this Model model, string colName, int colWidth, string additonalProperties) {
        dynamic expando = new ExpandoObject();
        var json = JsonConvert.DeserializeObject<JObject>(additonalProperties);

        expando.name = colName;
        expando.width = colWidth;

        return new FromPropertiesDynamicObjectCreator(expando, json);
    }

    private class FromPropertiesDynamicObjectCreator : DynamicObject
    {
        private readonly dynamic expando = null;

        public FromPropertiesDynamicObjectCreator(IDictionary<string, object> expando, JObject props = null) {
            this.expando = expando;

            if (props != null) {
                ((dynamic)this).props = props;
            }
        }

        public override bool TrySetMember(SetMemberBinder binder, object value) {
            if (binder.Name.Equals("props")) {
                var jsonObj = value as JObject;
                JToken current = jsonObj.First;
                var dictionary = expando as IDictionary<string, object>;

                RecurseJson(current, dictionary);
                return true;
            }

            return false;
        }

        private void RecurseJson(JToken current, IDictionary<string, object> dictionary) {
            JToken value;
            Dictionary<string, object> newDictionary;

            while (current != null) {
                var children = current.Children().ToList();

                foreach (var child in children) {
                    switch (child.Type) {

                        case JTokenType.Object:
                        case JTokenType.Array:
                            newDictionary = new Dictionary<string, object>();
                            dictionary[child.Path] = newDictionary;
                            RecurseJson(child, newDictionary);
                            break;

                        case JTokenType.Property:
                            var prop = ((JProperty)child);
                            value = prop.Value;

                            if (value.HasValues) {
                                newDictionary = new Dictionary<string, object>();
                                dictionary[prop.Name] = newDictionary;
                                RecurseJson(child, newDictionary);
                                break;
                            }
                            dictionary[prop.Name] = ((dynamic)value).Value;
                            break;

                        default:
                            var val = ((dynamic)child).Value;

                            if (val is JToken) {
                                dictionary[child.Path] = val.Value;
                            }
                            else {
                                dictionary[child.Path] = val;
                            }

                            break;
                    }
                }

                current = current.Next;
            }
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result) {
            object value;
            var dictionary = expando as IDictionary<string, object>;

            if (dictionary.TryGetValue(binder.Name, out value)) {
                var innerDictionary = value as IDictionary<string, object>;

                if (innerDictionary != null) {
                    result = new FromPropertiesDynamicObjectCreator(innerDictionary);
                }
                else {
                    result = value;
                }
                return true;
            }

            result = null;
            return true;
        }
    }
}
静态类模型扩展
{   
公共静态动态GetColModel(此模型模型、字符串colName、int colWidth、字符串附加属性){
动态expando=新的ExpandoObject();
var json=JsonConvert.DeserializeObject(additonalProperties);
expando.name=colName;
expando.width=colWidth;
从PropertiesDynamicObjectCreator(expando,json)返回新的属性;
}
Properties的私有类DynamicObjectCreator:DynamicObjector
{
私有只读动态expando=null;
public FromPropertiesDynamicObjectCreator(IDictionary expando,JObject props=null){
this.expando=expando;
如果(道具!=null){
((动态)这个)。道具=道具;
}
}
public override bool TrySetMember(SetMemberBinder绑定器,对象值){
if(binder.Name.Equals(“props”)){
var jsonObj=作为JObject的值;
JToken current=jsonObj.First;
var dictionary=expando作为IDictionary;
RecurseJson(当前,字典);
返回true;
}
返回false;
}
私有void RecurseJson(JToken current,IDictionary dictionary){
JToken值;
新字典;
while(当前!=null){
var children=current.children().ToList();
foreach(儿童中的儿童变量){
开关(子类型){
案例JTokenType.Object:
案例JTokenType.Array:
newDictionary=newDictionary();
dictionary[child.Path]=newDictionary;
RecurseJson(child,newDictionary);
打破
案例JTokenType.Property:
var prop=((JProperty)子对象);
价值=财产价值;
if(value.HasValues){
newDictionary=newDictionary();
字典[prop.Name]=新字典;
RecurseJson(child,newDictionary);
打破
}
字典[prop.Name]=((动态)值).value;
打破
违约:
var val=((动态)子项).Value;
如果(val为JToken){
字典[child.Path]=值;
}
否则{
字典[child.Path]=val;
}
打破
}
}
当前=当前。下一步;
}
}
公共重写bool TryGetMember(GetMemberBinder绑定器,输出对象结果){
目标价值;
var dictionary=expando作为IDictionary;
if(dictionary.TryGetValue(binder.Name,out值)){
var innerDictionary=作为IDictionary的值;
如果(innerDictionary!=null){
结果=新建FromPropertiesDynamicObjectCreator(innerDictionary);
}
否则{
结果=值;
}
返回true;
}
结果=空;
返回true;
}
}
}

我可以将附加属性作为json字符串传递,并使用json.NET解析器从中创建.NET对象。如何将这些属性添加到返回的对象中?是否有一些示例如何使用expando对象?没有具体的示例,但expando的要点是您可以动态添加属性。