Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1551 | Anzahl registrierter User: 1841 | Besucher online: 67
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

arbeiten mit einem struct-Typ. Auflisten, zählen


Autor: Krzysztof
Sprache: C#
Bewertung:
9.24 (4 votes)
Anzahl der Aufrufe: 4808
  
Kick it on dotnet-kicks.de  

Beschreibung:



mein Beispiel-Aufruf Liefert:
sMyStruct = "iId;sValue;oTag"
iCount = 3


Abgelegt unter: struct, Auflisten, zählen, FieldInfo, FieldType, csv, GetType.



C#
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;
	}
}


Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.