expandoobject

How to set ExpandoObject's dictionary as case insensitive?

依然范特西╮ 提交于 2020-01-23 06:26:41
问题 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 ? 回答1: You may checkout Massive's implementation of a MassiveExpando which is case insensitive

How to set ExpandoObject's dictionary as case insensitive?

﹥>﹥吖頭↗ 提交于 2020-01-23 06:26:27
问题 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 ? 回答1: You may checkout Massive's implementation of a MassiveExpando which is case insensitive

How to create a new unknown or dynamic/expando object in Python

守給你的承諾、 提交于 2020-01-22 08:20:06
问题 In python how can we create a new object without having a predefined Class and later dynamically add properties to it ? example: dynamic_object = Dynamic() dynamic_object.dynamic_property_a = "abc" dynamic_object.dynamic_property_b = "abcdefg" What is the best way to do it? EDIT Because many people advised in comments that I might not need this. The thing is that I have a function that serializes an object's properties. For that reason, I don't want to create an object of the expected class

Deserialize a property as an ExpandoObject using JSON.NET

被刻印的时光 ゝ 提交于 2020-01-21 04:06:32
问题 For example, there's an object like the next one: public class Container { public object Data { get; set; } } And it's used this way: Container container = new Container { Data = new Dictionary<string, object> { { "Text", "Hello world" } } }; If I deserialize a JSON string obtained from serializing the above instance, the Data property, even if I provide the ExpandoObjectConverter , it's not deserialized as an ExpandoObject : Container container = JsonConvert.Deserialize<Container>(jsonText,

check to see if a property exists within a C# Expando class

时光毁灭记忆、已成空白 提交于 2020-01-03 08:30:11
问题 I would like to see if a property exist in a C# Expando Class. much like the hasattr function in python. I would like the c# equalant for hasattr. something like this... if (HasAttr(model, "Id")) { # Do something with model.Id } 回答1: Try: dynamic yourExpando = new ExpandoObject(); if (((IDictionary<string, Object>)yourExpando).ContainsKey("Id")) { //Has property... } An ExpandoObject explicitly implements IDictionary<string, Object> , where the Key is a property name. You can then check to

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

半腔热情 提交于 2020-01-03 07:09: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

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

孤街浪徒 提交于 2020-01-02 00:53:07
问题 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

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

。_饼干妹妹 提交于 2019-12-29 09:09:48
问题 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

How to detect if a property exists on an ExpandoObject?

橙三吉。 提交于 2019-12-27 11:36:55
问题 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? 回答1: 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

C# Dynamic linq order by expando object

帅比萌擦擦* 提交于 2019-12-25 07:54:27
问题 I have a situation where I will need to sort dynamically based on a configuration from database table ascending or descending My Database table has column name That I will need to sort and sort order. I have a list of objects, in which each object has an ExpandoObject. My Class looks like this public class Customer: Base { public string Name { get; set;} public string City { get; set;} public string Address { get; set;} public string State { get; set;} public int Id { get; set;} public