expandoobject

Exposing properties of an ExpandoObject

好久不见. 提交于 2019-11-28 00:45:16
问题 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

C# 4.0 Dynamic vs Expando… where do they fit?

為{幸葍}努か 提交于 2019-11-27 18:56:58
I am trying to learn all the new goodies that come with C# 4.0. I am failing to understand the differences between the DynamicObject and ExpandoObject types. It seems like DynamicObject is used e.g. when you want to access variables from Python scripts and ExpandoObject when talking with COM/Office objects. Am I right? What is the difference in their use? Expando is a dynamic type to which members can be added (or removed) at runtime. dynamic is designed to allow .NET to interoperate with types when interfacing with dynamic typing languages such as Python and JavaScript. So, if you need to

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

微笑、不失礼 提交于 2019-11-27 07:35:48
问题 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

How do I dynamically generate columns in a WPF DataGrid?

删除回忆录丶 提交于 2019-11-27 06:45:01
I am attempting to display the results of a query in a WPF datagrid. The ItemsSource type I am binding to is IEnumerable<dynamic> . As the fields returned are not determined until runtime I don't know the type of the data until the query is evaluated. Each "row" is returned as an ExpandoObject with dynamic properties representing the fields. It was my hope that AutoGenerateColumns (like below) would be able to generate columns from an ExpandoObject like it does with a static type but it does not appear to. <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Results}"/> Is there anyway

Can I serialize an ExpandoObject in .NET 4?

自作多情 提交于 2019-11-27 05:42:31
问题 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

Differences between ExpandoObject, DynamicObject and dynamic

你说的曾经没有我的故事 提交于 2019-11-27 02:41:29
What are the differences between System.Dynamic.ExpandoObject , System.Dynamic.DynamicObject and dynamic ? In which situations do you use these types? SLaks The dynamic keyword is used to declare variables that should be late-bound. If you want to use late binding, for any real or imagined type, you use the dynamic keyword and the compiler does the rest. When you use the dynamic keyword to interact with a normal instance, the DLR performs late-bound calls to the instance's normal methods. The IDynamicMetaObjectProvider interface allows a class to take control of its late-bound behavior. When

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

天大地大妈咪最大 提交于 2019-11-27 01:26:36
问题 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

C# 4.0 Dynamic vs Expando… where do they fit?

99封情书 提交于 2019-11-26 22:47:08
问题 I am trying to learn all the new goodies that come with C# 4.0. I am failing to understand the differences between the DynamicObject and ExpandoObject types. It seems like DynamicObject is used e.g. when you want to access variables from Python scripts and ExpandoObject when talking with COM/Office objects. Am I right? What is the difference in their use? 回答1: Expando is a dynamic type to which members can be added (or removed) at runtime. dynamic is designed to allow .NET to interoperate

How to detect if a property exists on an ExpandoObject?

久未见 提交于 2019-11-26 18:22:19
In javascript you can detect if a property is defined by using the undefined keyword: if( typeof data.myProperty == "undefined" ) ... How would you do this in C# using the dynamic keyword with an ExpandoObject and without throwing an exception? According to MSDN the declaration shows it is implementing IDictionary: public sealed class ExpandoObject : IDynamicMetaObjectProvider, IDictionary<string, Object>, ICollection<KeyValuePair<string, Object>>, IEnumerable<KeyValuePair<string, Object>>, IEnumerable, INotifyPropertyChanged You can use this to see if a member is defined: var expandoObject =

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

天大地大妈咪最大 提交于 2019-11-26 18:22:07
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 below, to be consumed by the browser: [{"Key":"SomeProp", "Value": SomeValueOrClass}] BUT, what I'd really