Autor Beitrag
Glowhollow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 77



BeitragVerfasst: Di 23.10.18 16:00 
Hallo,

ich bin noch recht neu in Reflections, weiß aber, das man den Property Namen auslesen kann.

Wie stelle ich das am einfachsten an ?

Ich weiß auch, wie die Property heißt, muß also nicht suchen. Ich brauche keine "Suche Property mit dem Namen", sondern ich suche eine "Hole mir den Property namen und gib String zurück".

Hat da mir jemand ein Codeschnipsel ?
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 23.10.18 16:10 
Im groben
ausblenden C#-Quelltext
1:
meinLiebesObjekt.GetType().GetProperty("meinLieberPropertyName").GetValue(meinLiebesObjekt, null);					

Sollte es komplizierende Details geben z.B. Sichtbarkeit nicht public, Property ist ein Indexer und braucht deshalb Parameter oder sowas dann muß du da noch ein wenig dran feilen.
Glowhollow Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 77



BeitragVerfasst: Di 23.10.18 16:26 
ok, ich habe mich vielleicht etwas unklar ausgedrückt.

ich habe eine for each schleife, indem ich durch sämtliche Properties durchgehe. Jetzt möchte ich das element übergeben, und davon den Property Namen haben.

bislang habe ich...

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
public void getIncidentFromData(List<Objectvalues> data)
string INCNr = Incidentnr.Get(context);
{
 foreach (var elem in data)
 {
  if (elem.number == INCNr)
  {
  giveFunctionTheElement(elem);
  }
 }
}


und die funktion soll in etwa so aussehen

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
public string giveFunctionTheElement<T>(T elem)
 {
  Type t=elem.GetType();
  String elemname = t.GetProperties[0].ToString()
 }


leider funktioniert das nicht so, wie gedacht, wo ist der fehler ?
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 23.10.18 17:18 
Zitat:
ok, ich habe mich vielleicht etwas unklar ausgedrückt.


Gut erkannt :wink: Bleibt für mich aber leider unklar.

Warum GetProperties[0]? Meintest du vielleicht eher GetProperties()[0]? Beachte die Klammern.

Jenseits davon du bekommst an der Stelle ein PropertyInfo Object zurück. Und üblicherweise bekommt man beim Aufruf von ToString() irgendetwas zurück das beim debuggen hilft aber nicht sinnvoll im Code weiterhilft um damit zu arbeiten. Bei PropertyInfo kommt scheinbar ein Kombination aus Typ und Name der Property zurück (ist leider aktuell nicht dokumentiert was man da bekommt). Nichts was so direkt weiterhelfen sollte. Wenn du den Namen willst dann ruf Name auf und wenn du den Typ willst PropertyType. ToString sollte hier aber nie helfen.

Wenn du echte Hilfe willst solltest du noch ein wenig ausholen da ich das was du willst, wie gesagt, immer noch nicht ganz verstanden habe. Der Methoden Name giveFunctionTheElement klingt auch eher verdächtig. Vermutlich soll die Methode mehr bzw. was anderes tun.
Glowhollow Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 77



BeitragVerfasst: Mi 24.10.18 13:05 
hi,

ich habe das jetzt wiefolgt gelöst.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
public String buildOutput<T>(T source, String second, NativeActivityContext context)
{
Type t = source.GetType();
String sourcename = t.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)[1].Name.ToString();
String output = "{"+'"' + sourcename + '"'":" + '"'+ second + '"'"}";
return output;
}
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mi 24.10.18 13:37 
Die "1" in dem Code finde ich merkwürdig. Wenn da ein beliebiger Typ T reingeht. Wie kann dann bei allen diesen Typen die, mehr oder weniger zufällige, 2 Property dieses Typs irgendwie relevant sein? Wofür ist das gut? Ist T gar nicht so beliebig wie der Code behauptet?

Name ist bereits ein string. Da ToString aufzurufen hilft nicht.
Glowhollow Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 77



BeitragVerfasst: Mi 24.10.18 13:48 
Die "1" ist nur n platzhalter, natürlich habe ich das angepasst, habe ja die klassen und die properties übernommen bzw. erstellt. Im Grunde gehts darum, das ich einen JSON String erstellen muß und halt den namen der Property brauche, da ich das für eine REST API-Put Anwendung brauche. Mehr wars eigentlich nicht. Das mit den ToString stimmt, alte angewohnheit ;).

Vielen dank nochmal für eure Zeit.
erfahrener Neuling
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 233
Erhaltene Danke: 19

Win 7, Win 10
C#, ASP-MVC (VS 2017 Community), MS SQL, Firebird SQL
BeitragVerfasst: Mi 24.10.18 14:54 
Zitat:
Im Grunde gehts darum, das ich einen JSON String erstellen muß und halt den namen der Property

Warum benutzt du dann nicht den JSON-Serializer? Der ist doch genau für solche Zwecke!

z.B.
ausblenden C#-Quelltext
1:
string json = Newtonsoft.Json.JsonConvert.SerializeObject(<dein Objekt/DataModelklasse>,<deine JsonSerializerSettings>);					

Die Modelklasse modelierst du einfach nach den Anforderungen der API
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 24.10.18 16:01 
Das hat user profile iconGlowhollow im anderen Forum unter myCSharp.de - Klassen Properties verschwinden, Debug zeigt sie aber an. schon abgelehnt (und ist jetzt wohl u.a. deswegen hier).

@Glowhollow: Deine Methode ist sehr eigenartig. Einerseits ist sie generisch, andererseits benutzt du den generischen Parameter nur, um den Typ daraus zu extrahieren.

Warum also nicht
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
public string buildOutput<T>(string second, NativeActivityContext context)
{
  Type t = typeof(T);
  string sourcename = t.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)[1].Name);
  string output = "{"+'"' + sourcename + '"'":" + '"'+ second + '"'"}";
  return output;
}

oder
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
public String buildOutput(object source, String second, NativeActivityContext context)
{
  Type t = source.GetType();
  string sourcename = t.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)[1].Name);
  string output = "{"+'"' + sourcename + '"'":" + '"'+ second + '"'"}";
  return output;
}