expandoobject

Dynamically adding properties to an Object from an existing static object in C#

僤鯓⒐⒋嵵緔 提交于 2019-12-14 03:54:43
问题 In my ASP .Net Web API Application while making the DB calls, some properties are needed to be added to the Model Class which already have some existing properties. I understand I can use ExpandoObject in this case and add properties at run time, but I want to know how first to inherit all the properties from an existing object and then add a few. Suppose for example, the object that's being passed to the method is ConstituentNameInput and is defined as public class ConstituentNameInput {

Adding Interface Implementation to ExpandoObject

我们两清 提交于 2019-12-14 02:25:22
问题 I am passing information between a SQL database and a PLC using 3rd party OPC libraries. There are essentially two transactions. Information passed from the PLC to the SQL server is statically typed. Very specific data is captured by the PLC and passed to the SQL database. Information passed from the SQL server to the PLC is dynamically typed and may be limited to a single property or hundreds. ITransaction.cs public interface ITransaction : INotifyPropertyChanged { short Response { get; set;

ExpandoObject add Property with “extra”-code

烂漫一生 提交于 2019-12-12 02:48:06
问题 I've read some stuff about the ExpandoObject, and that I can expand it with properties,fields,methods. //that's how to add a property to an ExpandoObject. dynamic x = new ExpandoObject(); x.NewProp = string.Empty; But sometimes, it could be handy to add a property with some "extra-code". class sample { // a sample field. public string sampleString{get;set} // a sample property with some "extra code" private string s; public string sampleExtraString { get{return s;} set{s=value;Console

How to add dynamic member name and value to an instance of ExpandoObject class?

心已入冬 提交于 2019-12-11 10:36:43
问题 Let’s say list of member names stored in an array like below, string[] myMembers = { "ChapterName", "Medium", "LastName", "OrderID" }; I wrote the following code to generate dynamic class and members along with random values. var myDynamicClassList = new List<ExpandoObject>(); foreach (var MemberName in myMembers) { dynamic dynamicClass = new ExpandoObject(); dynamicClass.MemberName = new Random().Next(); myDynamicClassList.Add(dynamicClass); } In order to display output of the above

Is it possible to access Expandoobject properties by index?

扶醉桌前 提交于 2019-12-11 09:11:25
问题 It is possible to override the TryGetIndex method of a dynamic object to access the dynamic object properties by index however I am dealing with an Expandoobject (of the System.dynamic namespace) which you can't inherit from. Is there a way around this? Thanks 回答1: ExpandoObject is nothing but a fancy IDictionary which leverages the DLR. There is no way you can access a IDictionary<TKey,TValue> via index. You may find ElementAt method of linq useful, but it is not. There is no ordering in

Servicestack ORMLite/Massive managing multiple DataTables with Expandos / Dynamic?

假如想象 提交于 2019-12-11 00:07:35
问题 i have a Stored Procedure that returns multiple datatables with dynamic types according to the input and I cannot modify or split it. I actually retrieve the data in this way: var massiveModel = new DynamicModel(dbConn.ConnectionString); var connection = new SqlConnection(@"Data Source=127.0.0.1;Initial Catalog=TEST;User ID=as;Password=;Application Name=BRUCE_WAYNE"); connection.Open(); var massiveConnection = connection; var tmp = massiveModel.Query("exec MY_SP 4412 '20131016' ",

Serialize instance of a class deriving from DynamicObject class

拈花ヽ惹草 提交于 2019-12-10 19:05:00
问题 I have a class that derives from DynamicObject class. On calling JsonConvert.SertializeObject, none of the dynamic properties are serialized. Class is defined as, public class Component : DynamicObject { // The inner dictionary. public Dictionary<string, object> dictionary = new Dictionary<string, object>(); [JsonProperty(PropertyName = "id")] public string Id { get; set; } // If you try to get a value of a property // not defined in the class, this method is called. public override bool

JSON array to ExpandoObject via JSON.NET

穿精又带淫゛_ 提交于 2019-12-10 17:47:40
问题 I am using the following approach to convert most of my API JSON results into an object: public void ExpandoObject() { var sampleDATA = Sample.Create(); var json = JsonConvert.SerializeObject(sampleDATA); var expConverter = new ExpandoObjectConverter(); dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json, expConverter); var a = obj.A; var b = obj.B; var c = obj.C; //and so on... } However, I run into a strange situation with this format of JSON... [ { "id": 42, "name": "example

Convert ExpandoObject to anonymous type

邮差的信 提交于 2019-12-07 14:07:18
问题 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

Is it possible to query list of ExpandoObject?

独自空忆成欢 提交于 2019-12-07 04:49:55
问题 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