expandoobject

Reflect on an ExpandoObject

三世轮回 提交于 2019-12-07 03:57:29
问题 I have written a nifty function that will accept a system.object , reflect on its properties and serialize the object into a JSON string. It looks like this: public class JSONSerializer { public string Serialize(object obj) Now, I want to be able to do this to serialize a dynamic/ExpandoObject, but because my serializer uses reflection, it isn't able to do it. What's the workaround? public class Test { public dynamic MakeDynamicCat() { dynamic newCat = new ExpandoObject(); newCat.Name =

How to serialize ExpandoObject using ServiceStack JsonSerializer?

别等时光非礼了梦想. 提交于 2019-12-07 02:32:23
问题 Is it possible to get the ServiceStack JsonSerializer to serialize an ExpandoObject as a flat object rather than a dictionary? Something roughly approximate to this: {"x":"xvalue","y":"\/Date(1313966045485)\/"} I am trying to compare JSON serialization of ExpandoObject using three different systems: the .NET BCL JavaScriptSerializer, Newtonsoft JSON.NET, and ServiceStack's JSON offering. I start with a fairly simple dynamic object. dynamic test = new ExpandoObject(); test.x = "xvalue"; test.y

Convert ExpandoObject to anonymous type

你说的曾经没有我的故事 提交于 2019-12-06 01:34:27
Is it possible to convert ExpandoObject to anonymously typed object? 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

C# deep/nested/recursive merge of dynamic/expando objects

て烟熏妆下的殇ゞ 提交于 2019-12-05 21:54:49
问题 I need to "merge" 2 dynamic objects in C#. All that I've found on stackexchange covered only non-recursive merging. But I am looking to something that does recursive or deep merging, very much the same like jQuery's $.extend(obj1, obj2) function. Upon collision of two members, the following rules should apply: If the types mismatch, an exception must be thrown and merge is aborted. Exception: obj2 Value maybe null, in this case the value & type of obj1 is used. For trivial types (value types

'System.Collections.Generic.IList<object>' does not contain a definition for 'Add' when using 'dynamic' and 'ExpandoObject'

独自空忆成欢 提交于 2019-12-05 12:51:11
问题 Say that I have a class, Foo, looking something like this: public class Foo : IFoo { public Foo() { Bars = new List<dynamic>(); } public IList<dynamic> Bars { get; set; } } The interface IFoo looks like: public interface IFoo { IList<dynamic> Bars { get; set; } } Now, when I do the following: IFoo foo = new Foo(); dynamic a = new System.Dynamic.ExpandoObject(); a.Prop = 10000; dynamic b = new System.Dynamic.ExpandoObject(); b.Prop = "Some Text"; foo.Bars.Add(a); // Throws an 'System

Is it possible to query list of ExpandoObject?

為{幸葍}努か 提交于 2019-12-05 08:52:00
I wonder if it is possible to query ExpandoObject with regular LINQ? Reason is that I have dynamic ExpandoObject but I need to do some querying before I can pass further. It has some properties that always stay e.g. Id , Notes but also some dynamic properties that I cannot control. Here is how my list of them looks like [ { "Id": 1, "FileId": 1, "Notes": "", "1": "12.02.1991" }, { "Id": 2, "FileId": 2, "Notes": "", "1": "12.02.1991" } ] Code As you can see I have my static items and then I make sure that every item dynamic keys become that item properties. In this example 1 is key and 12.02

How to set ExpandoObject's dictionary as case insensitive?

旧时模样 提交于 2019-12-05 07:30:46
given the code below dynamic e = new ExpandoObject(); var d = e as IDictionary<string, object>; for (int i = 0; i < rdr.FieldCount; i++) d.Add(rdr.GetName(i), DBNull.Value.Equals(rdr[i]) ? null : rdr[i]); Is there a way to make it case insensitive so given the field name employee_name e.Employee_name works just as well as e.employee_name there doesn't seem to be an obvious way, perhaps a hack ? You may checkout Massive's implementation of a MassiveExpando which is case insensitive dynamic object. Luke Sampson I've been using this “Flexpando” class (for flexible expando) which is case

Reflect on an ExpandoObject

 ̄綄美尐妖づ 提交于 2019-12-05 06:48:34
I have written a nifty function that will accept a system.object , reflect on its properties and serialize the object into a JSON string. It looks like this: public class JSONSerializer { public string Serialize(object obj) Now, I want to be able to do this to serialize a dynamic/ExpandoObject, but because my serializer uses reflection, it isn't able to do it. What's the workaround? public class Test { public dynamic MakeDynamicCat() { dynamic newCat = new ExpandoObject(); newCat.Name = "Polly"; newCat.Pedigree = new ExpandoObject(); newCat.Pedigree.Breed = "Whatever"; return newCat; } public

How to serialize ExpandoObject using ServiceStack JsonSerializer?

落爺英雄遲暮 提交于 2019-12-05 06:35:06
Is it possible to get the ServiceStack JsonSerializer to serialize an ExpandoObject as a flat object rather than a dictionary? Something roughly approximate to this: {"x":"xvalue","y":"\/Date(1313966045485)\/"} I am trying to compare JSON serialization of ExpandoObject using three different systems: the .NET BCL JavaScriptSerializer, Newtonsoft JSON.NET, and ServiceStack's JSON offering. I start with a fairly simple dynamic object. dynamic test = new ExpandoObject(); test.x = "xvalue"; test.y = DateTime.Now; It seems simpler for a serializer to treat an ExpandoObject as a IDictionary<string,

Using LINQ, is it possible to output a dynamic object from a Select statement? If so, how?

巧了我就是萌 提交于 2019-12-04 22:30:46
Presently in LINQ, the following compiles and works just fine: var listOfFoo = myData.Select(x => new FooModel{ someProperty = x.prop1, someOtherProperty = x.prop2 }); public class FooModel{ public string someProperty { get; set; }; public string someOtherProperty { get; set; }; } However, the past few versions of .NET/C# have expanded the role of dynamic objects such as the ExpandoObject and I am wondering if there is a way to basically do this: var listOfFoo = myData.Select(x => new ExpandoObject{ someProperty = x.prop1, someOtherProperty = x.prop2 }); Obviously, I have already tried the