Antworten:
Betrachtung; für eine Instanz:
obj.GetType().GetProperties();
für einen Typ:
typeof(Foo).GetProperties();
zum Beispiel:
class Foo {
public int A {get;set;}
public string B {get;set;}
}
...
Foo foo = new Foo {A = 1, B = "abc"};
foreach(var prop in foo.GetType().GetProperties()) {
Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(foo, null));
}
Nach Feedback ...
nullals erstes Argument an, um den Wert statischer Eigenschaften abzurufenGetValueGetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)(das alle öffentlichen / privaten Instanzeigenschaften zurückgibt).internalEigenschaften. Vielleicht bin ich der einzige, der an der private/ non-public-Syntax hängen geblieben ist ?
using System.ReflectionDirektive und das System.Reflection.TypeExtensionsPaket verwiesen wird - dies stellt die fehlende API-Oberfläche über Erweiterungsmethoden
Sie können Reflection verwenden , um dies zu tun: (aus meiner Bibliothek - dies erhält die Namen und Werte)
public static Dictionary<string, object> DictionaryFromType(object atype)
{
if (atype == null) return new Dictionary<string, object>();
Type t = atype.GetType();
PropertyInfo[] props = t.GetProperties();
Dictionary<string, object> dict = new Dictionary<string, object>();
foreach (PropertyInfo prp in props)
{
object value = prp.GetValue(atype, new object[]{});
dict.Add(prp.Name, value);
}
return dict;
}
Dieses Ding funktioniert nicht für Eigenschaften mit einem Index - dafür (es wird unhandlich):
public static Dictionary<string, object> DictionaryFromType(object atype,
Dictionary<string, object[]> indexers)
{
/* replace GetValue() call above with: */
object value = prp.GetValue(atype, ((indexers.ContainsKey(prp.Name)?indexers[prp.Name]:new string[]{});
}
So erhalten Sie nur öffentliche Eigenschaften: ( siehe MSDN in der BindingFlags-Enumeration )
/* replace */
PropertyInfo[] props = t.GetProperties();
/* with */
PropertyInfo[] props = t.GetProperties(BindingFlags.Public)
Dies funktioniert auch bei anonymen Typen!
Um nur die Namen zu bekommen:
public static string[] PropertiesFromType(object atype)
{
if (atype == null) return new string[] {};
Type t = atype.GetType();
PropertyInfo[] props = t.GetProperties();
List<string> propNames = new List<string>();
foreach (PropertyInfo prp in props)
{
propNames.Add(prp.Name);
}
return propNames.ToArray();
}
Und es ist ungefähr das gleiche für nur die Werte, oder Sie können verwenden:
GetDictionaryFromType().Keys
// or
GetDictionaryFromType().Values
Aber das ist etwas langsamer, würde ich mir vorstellen.
t.GetProperties(BindingFlags.Instance | BindingFlags.Public)odert.GetProperties(BindingFlags.Static | BindingFlags.Public)
public List<string> GetPropertiesNameOfClass(object pObject)
{
List<string> propertyList = new List<string>();
if (pObject != null)
{
foreach (var prop in pObject.GetType().GetProperties())
{
propertyList.Add(prop.Name);
}
}
return propertyList;
}
Diese Funktion dient zum Abrufen einer Liste der Klasseneigenschaften.
yield return. Es ist keine große Sache, aber es ist eine bessere Art, es zu tun.
Sie könnten den System.ReflectionNamespace mit dem Type.GetProperties()mehod verwenden:
PropertyInfo[] propertyInfos;
propertyInfos = typeof(MyClass).GetProperties(BindingFlags.Public|BindingFlags.Static);
Das ist meine Lösung
public class MyObject
{
public string value1 { get; set; }
public string value2 { get; set; }
public PropertyInfo[] GetProperties()
{
try
{
return this.GetType().GetProperties();
}
catch (Exception ex)
{
throw ex;
}
}
public PropertyInfo GetByParameterName(string ParameterName)
{
try
{
return this.GetType().GetProperties().FirstOrDefault(x => x.Name == ParameterName);
}
catch (Exception ex)
{
throw ex;
}
}
public static MyObject SetValue(MyObject obj, string parameterName,object parameterValue)
{
try
{
obj.GetType().GetProperties().FirstOrDefault(x => x.Name == parameterName).SetValue(obj, parameterValue);
return obj;
}
catch (Exception ex)
{
throw ex;
}
}
}
Ich stehe auch vor solchen Anforderungen.
Aus dieser Diskussion habe ich eine andere Idee,
Obj.GetType().GetProperties()[0].Name
Hier wird auch der Eigenschaftsname angezeigt.
Obj.GetType().GetProperties().Count();
Dies zeigt die Anzahl der Eigenschaften.
Dank an alle. Das ist eine nette Diskussion.
Hier ist @lucasjones Antwort verbessert. Ich habe Verbesserungen hinzugefügt, die im Kommentarbereich nach seiner Antwort erwähnt wurden. Ich hoffe, jemand wird dies nützlich finden.
public static string[] GetTypePropertyNames(object classObject, BindingFlags bindingFlags)
{
if (classObject == null)
{
throw new ArgumentNullException(nameof(classObject));
}
var type = classObject.GetType();
var propertyInfos = type.GetProperties(bindingFlags);
return propertyInfos.Select(propertyInfo => propertyInfo.Name).ToArray();
}