How you bosses, inquire of c #, according to the structure of the index number (index number corresponding to the number of structure), and use the corresponding data structure to save, thank you very much,
CodePudding user response:
How you bosses, inquire of c #, according to the structure of the index number (index number corresponding to the number of structure), output corresponding to the structure of data, many thanks, Semantics is not clear, rewriting the problem
CodePudding user response:
Don't write their own a Factory, is not lazy
Sometimes can't steal lazy, is a great god can steal lazy, or Google protocol buffer, facebook Grpahml.net version won't be that kind of awkward format
Give you a another great spirit level protocol used to automatically generate the object (Avro)
public partial class CatlogItem: ISpecificRecord { Public static Schema _SCHEMA=Avro. Schema. Parse (" {\ "type " : \ "record ", \ "name " : \ "CatlogItem ", \ "namespace " : \ "Smart. Protocol ", \ "the field" + "S " : [{\ "name " : \ "CatlogID ", \ "type " : \ "string "}, {\ "name " : \ "CatlogName ", \ "type " : \ "string \}]}"); Private string _CatlogID; Private string _CatlogName; Public virtual Schema Schema { The get { Return CatlogItem. _SCHEMA; } } Public string CatlogID { The get { Return this. _CatlogID; } Set { Enclosing _CatlogID=value; } } Public string CatlogName { The get { Return this. _CatlogName; } Set { Enclosing _CatlogName=value; } } Public virtual object Get (int fieldPos) { The switch (fieldPos) { Case 0: return this. CatlogID; Case 1: return this. CatlogName; Default: throw new AvroRuntimeException (" Bad index "+ fieldPos +" in the Get () "); }; } Public virtual void Put (int fieldPos, object fieldValue) { The switch (fieldPos) { Case 0: this. CatlogID=(System. String) fieldValue; break; Case 1: enclosing CatlogName=(System. String) fieldValue; break; Default: throw new AvroRuntimeException (" Bad index "+ fieldPos +" in the Put () "); }; } }
Saw him trying on index is not, of course, this kind of thing in Avro, Google is tool automatically generated in the protocol buffer, we don't write manually, then you can see, if there is a very simple trying, the great god level tool also won't get into this weird writing
Ps: according to your description, in fact, I think, would you like to do more rather than a Factory Factory index
CodePudding user response:
If there is a way to just forced to write, and just a little uncomfortable point Get a extension methods, according to the offset row a sequence
public static class StructEx { Public static object GetFiledValueByIndex (this T obj, int index) Where T: struct { Var LST=obj. GetType (). The GetFields () OrderBy (p=& gt; {return Marshal. OffsetOf P.N (ame). ToInt32 (); });
If (index & gt;=LST. The Count ()) { Throw new Exception (" index cross-border "); }