expandoobject

Dynamically adding properties to a dynamic object?

此生再无相见时 提交于 2019-11-30 03:00:34
问题 i have this dynamic d = new ExpandoObject(); d.Name = attribute.QualifiedName.Name; so , i know that d will have a property Name. Now if i don't know the name of the property at compile time , how do i add that property to the dynamic. i found this SO Question so, there is this complicated concept of call binders etc..which is tough to get in the first place.any simpler way of doing this ? 回答1: dynamic d = new ExpandoObject(); ((IDictionary<string,object>)d)["test"] = 1; //now you have d.test

Cast ExpandoObject to anonymous type

独自空忆成欢 提交于 2019-11-30 01:50:36
问题 Can I cast ExpandoObject to anonymous type ? var anoObj = new { name = "testName", email = "testEmail" }; dynamic expandoObj = new System.Dynamic.ExpandoObject(); // Here I'm populating the expandoObj with same property names/types in anonymoustype(anoObj) // Now, how to convert this ExpandoObject to anonymoustype ? var newObj = (typeof(anoObj)expandoObj); // This doesn't work Added Later // This is my entity public class Customer { #region Public Properties [ColumnAttribute(Name = "IdColumn"

Accessing properties of anonymous/dynamic types across dll boundaries gives RuntimeBinderException

走远了吗. 提交于 2019-11-29 15:40:55
In the following sample, x.propertyX works fine, whereas y.propertyX gives me a Microsoft.CSharp.RuntimeBinder.RuntimeBinderException , complaining 'propertyX' is not defined in 'object'. The CreateDynamic method in the Program class (shown below) and the one in Class1 (not shown) are exactly the same, but Class1 is in a different project from Program. If I move Class1 into Program's project, everything works fine. class Program { public static object CreateDynamic() { return new { propertyX = "asdf" }; } static void Main(string[] args) { dynamic x = CreateDynamic(); Console.WriteLine(x

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

隐身守侯 提交于 2019-11-29 09:36:49
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 property, and then add my properties to the parent ExpandoObject. Thanks JCL, I was in brain-freeze as to

Why can't I index into an ExpandoObject?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 09:04:04
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 have looked at the documentation for ExpandoObject and it explicitly implements IDictionary<,> and thus

Exposing properties of an ExpandoObject

匆匆过客 提交于 2019-11-29 06:58:34
I've got an ExpandoObject that I'm sending to an external library method which takes an object. From what I've seen this external lib uses TypeDescriptor.GetProperties internally and that seems to cause some problems with my ExpandoObject. I could go with an anonymous object instead and that seems to work but it much more convenient for me to use the ExpandoObject. Do I need to construct my own DynamicObject and take care of it myself by implementing ICustomTypeDescriptor or am I missing something here. Ideas? Update Besides the answer by somedave below (as per the comments), I added this

In C#, how do I remove a property from an ExpandoObject?

梦想的初衷 提交于 2019-11-29 05:41:24
问题 Say I have this object: dynamic foo = new ExpandoObject(); foo.bar = "fizz"; foo.bang = "buzz"; How would I remove foo.bang for example? I don't want to simply set the property's value to null--for my purposes I need to remove it altogether. Also, I realize that I could create a whole new ExpandoObject by drawing kv pairs from the first, but that would be pretty inefficient. 回答1: Cast the expando to IDictionary<string, object> and call Remove: var dict = (IDictionary<string, object>)foo; dict

.NET 4.0 framework dynamic features in VB with Option Strict On?

孤街浪徒 提交于 2019-11-28 13:18:12
Is there any way to use the new dynamic features in the 4.0 framework like ExpandoObject in VB.NET without setting Option Strict Off ? With C#, you lose type safety only with the variables you specifically declare as dynamic . But with VB, the only way I've found to use these features is with the old Option Strict Off trick that's been in VB.NET since the beginning. Without Option Strict, everything in the file is polluted with fuzzy typing like so: Option Explicit On Option Strict Off Option Infer On Partial Public Class ClassX Public Sub TestDynamic() Dim dyn As Object = New System.Dynamic

Can I serialize an ExpandoObject in .NET 4?

做~自己de王妃 提交于 2019-11-28 06:48:39
I'm trying to use a System.Dynamic.ExpandoObject so I can dynamically create properties at runtime. Later, I need to pass an instance of this object and the mechanism used requires serialization. Of course, when I attempt to serialize my dynamic object, I get the exception: System.Runtime.Serialization.SerializationException was unhandled. Type 'System.Dynamic.ExpandoObject' in Assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable. Can I serialize the ExpandoObject? Is there another approach to creating a dynamic object that is

Why can't I do this: dynamic x = new ExpandoObject { Foo = 12, Bar = “twelve” }

做~自己de王妃 提交于 2019-11-28 06:40:47
Am I doing something wrong, or is the following code really not possible? dynamic x = new ExpandoObject { Foo = 12, Bar = "twelve" }; If this really isn't possible, is there another one-line way to instantiate an ExpandoObject with two properties? Why would the C# team opt to disallow the same initialization syntax as for regular objects, anonymous objects, and enumerables/lists? Update I asked this question because I was trying show a Pearl enthusiast the cool new dynamic features of C#, but then I was stalled by not being able to do what I thought was a logical instantiation of an