C# dictionary multiple classes as values -


i'm searching way create dictionary accepte multiple classe value.

i'm getting value xiaomi gateway , foreach kind of equipment, i've got class each kind of equipement.

for example magnet sensor :

[xiaomiequipement("magnet")] public class magnet {     public string model { get; set; } = "magnet";     public string sid { get; set; }     public string battery { get; set; } = "cr1632";     public int batterylevel { get; set; }     public magnetreport report { get; set; } }  [xiaomiequipement("magnet_report")] public class magnetreport {     public int voltage { get; set; }     public status { get; set; } } 

and wallplug :

[xiaomiequipement("plug")] public class plug {     public string model { get; set; } = "plug";     public string sid { get; set; }     public string battery { get; set; } = "cr2450";     public int batterylevel { get; set; }     public plugreport report { get; set; } }  [xiaomiequipement("plug_report")] public class plugreport {     public int voltage { get; set; }     public string status { get; set; }       } 

xiaomi gateway send 2 kind of data, report when happen , heartbeat every x minutes.

{"cmd":"heartbeat","model":"plug","sid":"158d000123f0c9","short_id":11119,"data":"{\"voltage\":3600,\"status\":\"off\",\"inuse\":\"0\",\"power_consumed\":\"7\",\"load_power\":\"0.00\"}"}

{"cmd":"report","model":"plug","sid":"158d000123f0c9","short_id":11119,"data":"{\"status\":\"on\"}"}

as can see, 2 lines haven't same data. want init plug class , after fill missing data when heartbeat or report arrive.

i use activator.createinstance sensor type create right class.

modeltype = assembly.getexecutingassembly().gettypes().singleordefault(t => t.getcustomattribute<response.xiaomiequipementattribute>()?.model == read.model); modelreporttype = assembly.getexecutingassembly().gettypes().singleordefault(t => t.getcustomattribute<response.xiaomiequipementattribute>()?.model == read.model + "_report"); 

i try use dictionary store sensor data edit after en heartbeat doesn't work every sensor has different class.

i try add interface, work doesn't work report class.

how include these classes dictionary , access ?

basically want search dictionary key, value , change part of it.

you may use dictionary<string, object> store them.

var items = new dictionary<string, object>(); var mag = new magnet() { sid = "1" }; var mot = new motion() { sid = "2" }; items.add(mag.sid, new magnet()); items.add(mot.sid, new motion()); 

then can determine type using object.gettype() this:

foreach (var thisitem in items) {     // or use thisitem.value magnet     if (thisitem.value.gettype().name == "magnet")     {         console.writeline("magnet");     }     else     {         console.writeline("motion");     } } 

you can put common properties base class , inherit 2 classes base class.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -