mein Beispiel-Aufruf Liefert:
sMyStruct = "iId;sValue;oTag"
iCount = 3
public struct MyStruct
{
public int iId;
public string sValue;
public object oTag;
}
//---------------------
// so kann es aussehen
MyStruct oStruktur = new MyStruct();
oStruktur.iId = 1;
oStruktur.sValue = "Test";
StructClass oStructCls = new StructClass();
string sMyStruct = oStructCls.ToCsvList((object)oStruktur);
int iCount = oStructCls.Count((object)oStruktur);
//---------------------------------------
class StructClass
{
// Anzahl der Elemente in einer struct
public int Count(object oStruct)
{
int iCount = 0;
System.Type oType = oStruct.GetType();
foreach (FieldInfo oFi in oType.GetFields())
{
iCount++;
}
return iCount;
}
public string ToCsvList(object oStruct)
{
string sCsv = "";
System.Type oType = oStruct.GetType();
foreach (FieldInfo oFi in oType.GetFields())
{
sCsv += ";" + oFi.Name;
System.Type feldTyp = oFi.FieldType; //z.B.: System.string
}
sCsv = sCsv.Substring(1);
return sCsv;
}
}
Kommentare zum Snippet