1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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;
}
}
|