Home > Net >  C # value structure
C # value structure

Time:03-10

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 ");
}

Return LST. ElementAt (index). GetValue (obj);


}
}

CodePudding user response:

reference wanghui0380 reply: 3/f
if just forced to write, also have a way, 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 ");
}

Return LST. ElementAt (index). GetValue (obj);


}
}

Code, thank you very much, I understand a little meaning, is a novice to see more long, please forgive me,

You see ah, such a structure that part roughly means, will structure the content in the memory, constantly updated, I write the function of this code is called structure of data, to do the next step, according to my idea, the structure of a unique element as reference conditions, comply with the conditions, is used when the elements of all data, to complete the function of this part, I don't know so understand right.
I'm afraid I can't explain clearly, another way, the structure itself has an element in the body, from 0-100 sorting, convenient call, the caller's own cycle, from 0 to 100, only need to remove the structure elements in the judgment, with equal, not equal to continue to look for,

This problem is to want to see if there is a direct structure has been obtained by an element can all data method, is really lazy,

CodePudding user response:

refer to the second floor wanghui0380 response:
write their own a Factory, not stealing 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)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related