expandoobject

How to extend a method at runtime?

六月ゝ 毕业季﹏ 提交于 2019-12-25 04:00:19
问题 Here is the class: class Foo { private void Boo() { // Body... } // Other members... } What I need is: Create a Foo2 class at runtime which has a copy of all Foo class members. In Foo2 class replace Boo method by Boo2 method that has its content alternated to some extent. Create an instance of Foo2 and invoke Boo2 . Thank you for help. 回答1: You can do it at runtime using a .NET AOP Framework event if it is not the the main purpose of this kind of framework. I actively work on a new one which

Allow multiple values for the same key when creating JSON using ExpandoObject and IDictionary

梦想的初衷 提交于 2019-12-24 11:21:40
问题 I am trying to create dynamic JSON using ExpandoObject and IDictionary. During the dynamic creation of JSON there could be instances when the Name or Value would repeat. However when adding the repeated Name or Value to the ExpandoObject, it gives an error: An item with the same key has already been added. Below is my code snippet : DataTable dt_MappedColumns = (DataTable)ViewState["MappedColumns"]; dynamic ManCols = new ExpandoObject(); var dictionary1 = (IDictionary<string, object>)ManCols;

ExpandoObject object and GetProperty()

倾然丶 夕夏残阳落幕 提交于 2019-12-24 03:58:09
问题 I'm trying to write a generic utility for use via COM from outside .NET (/skip long story). Anyway, I'm trying to add properties to an ExpandoObject and I need to get PropertyInfo structure back to pass to another routine. using System.Collections.Generic; using System.Diagnostics; using System.Dynamic; using System.Reflection; public class ExpandoTest { public string testThis(string cVariable) { string cOut = ""; ExpandoObject oRec = new ExpandoObject { }; IDictionary<string, object> oDict =

How can I use collection initializer syntax with ExpandoObject?

徘徊边缘 提交于 2019-12-20 18:49:07
问题 I've noticed that the new ExpandoObject implements IDictionary<string,object> which has the requisite IEnumerable<KeyValuePair<string, object>> and Add(string, object) methods and so it should be possible to use the collection initialiser syntax to add properties to the expando object in the same way as you add items to a dictionary. Dictionary<string,object> dict = new Dictionary<string,object>() { { "Hello", "World" } }; dynamic obj = new ExpandoObject() { { "foo", "hello" }, { "bar", 42 },

Add property to ExpandoObject with the same name as a string

一个人想着一个人 提交于 2019-12-20 10:01:50
问题 Is there a way to add a property to an ExpandoObject with the same name as a string value? For example, if I have: string propName = "ProductNumber"; dynamic obj = new System.Dynamic.ExpandoObject(); I can create the property ProductNumber like: obj.ProductNumber = 123; But, can I create the property obj.ProductNumber based on the string propName ? So, if I don't know what the name of the property will be in advanced, I can create it based on this input. If this is not possible with

Is there a way to perform a chained null check in a dynamic/expando?

我怕爱的太早我们不能终老 提交于 2019-12-19 10:22:11
问题 C# has the usefull Null Conditional Operator. Well explained in this answer too. I was wondering if it is possible to do a similar check like this when my object is a dynamic/expando object. Let me show you some code: Given this class hierarchy public class ClsLevel1 { public ClsLevel2 ClsLevel2 { get; set; } public ClsLevel1() { this.ClsLevel2 = new ClsLevel2(); // You can comment this line to test } } public class ClsLevel2 { public ClsLevel3 ClsLevel3 { get; set; } public ClsLevel2() {

ExpandoObject, anonymous types and Razor

两盒软妹~` 提交于 2019-12-18 17:35:33
问题 I want to use an ExpandoObject as the viewmodel for a Razor view of type ViewPage<dynamic> . I get an error when I do this ExpandoObject o = new ExpandoObject(); o.stuff = new { Foo = "bar" }; return View(o); what can I do to make this work? 回答1: You can do it with the extension method mentioned in this question: Dynamic Anonymous type in Razor causes RuntimeBinderException So your controller code would look like: dynamic o = new ExpandoObject(); o.Stuff = new { Foo = "Bar" }.ToExpando();

can one convert a dynamic object to an ExpandoObject (c#)

独自空忆成欢 提交于 2019-12-18 05:49:12
问题 I am getting an dynamic object of type "Sealed Class" from a driver api (in dll). I want to decorate this object with a few additional properties. I would like to do something to the effect of: public void expandIT(dynamic sealedObject) { ExpandoObject expand = new ExpandoObject(sealedObject); expand.time = DateTime.Now(); etc.... } UPDATE I like JCL's solution. But for what I wanted to do, it was easier to create a ExpandoObject and then embed the Dynamic sealed class object as a child

Why can't I index into an ExpandoObject?

谁说胖子不能爱 提交于 2019-12-18 05:27:11
问题 Something caught me by surprise when looking into C# dynamics today (I've never used them much, but lately I've been experimenting with the Nancy web framework). I found that I couldn't do this: dynamic expando = new ExpandoObject(); expando.name = "John"; Console.WriteLine(expando["name"]); The last line throws an exception: Cannot apply indexing with [] to an expression of type 'System.Dynamic.ExpandoObject' I understand the error message, but I don't understand why this is happening. I

How to flatten an ExpandoObject returned via JsonResult in asp.net mvc?

耗尽温柔 提交于 2019-12-17 04:18:10
问题 I really like the ExpandoObject while compiling a server-side dynamic object at runtime, but I am having trouble flattening this thing out during JSON serialization. First, I instantiate the object: dynamic expando = new ExpandoObject(); var d = expando as IDictionary<string, object>; expando.Add("SomeProp", SomeValueOrClass); So far so good. In my MVC controller, I want to then send this down as a JsonResult, so I do this: return new JsonResult(expando); This serializes the JSON into the