Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting ALL the properties of an object [duplicate]

Tags:

c#

i have object like this:

some_object

this object has like 1000 properties.

i would like to loop through every property like this:

foreach (property in some_object)
//output the property

is there an easy way to do this?

like image 894
Alex Gordon Avatar asked Jun 22 '11 18:06

Alex Gordon


People also ask

How do I print all properties of an object?

First way to print all properties of person object is by using Object. keys() method. In this method we pass the person object to Object. keys() as an argument.

How do you copy properties from one object to another in JavaScript?

The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.


6 Answers

You can use reflection.

// Get property array
var properties = GetProperties(some_object);

foreach (var p in properties)
{
    string name = p.Name;
    var value = p.GetValue(some_object, null);
}

private static PropertyInfo[] GetProperties(object obj)
{
    return obj.GetType().GetProperties();
}

However, this still does not solve the problem where you have an object with 1000 properties.

like image 138
Chris Trombley Avatar answered Sep 22 '22 17:09

Chris Trombley


Another approach you can use in this situation is converting an object into a JSON object. The JSON.NET library makes this easy and almost any object can be represented in JSON. You can then loop through the objects properties as Name / Value pairs. This approach would be useful for composite objects which contain other objects as you can loop through them in a tree-like nature.

MyClass some_object = new MyClass() { PropA = "A", PropB = "B", PropC = "C" };
JObject json = JObject.FromObject(some_object);
foreach (JProperty property in json.Properties())
    Console.WriteLine(property.Name + " - " + property.Value);

Console.ReadLine();
like image 24
Despertar Avatar answered Sep 24 '22 17:09

Despertar


using System.Reflection;  // reflection namespace

// get all public static properties of MyClass type
PropertyInfo[] propertyInfos;
propertyInfos = typeof(MyClass).GetProperties(BindingFlags.Public |
                                              BindingFlags.Static);
// sort properties by name
Array.Sort(propertyInfos,
        delegate(PropertyInfo propertyInfo1, PropertyInfo propertyInfo2)
        { return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });

// write property names
foreach (PropertyInfo propertyInfo in propertyInfos)
{
  Console.WriteLine(propertyInfo.Name);
}

Source: http://www.csharp-examples.net/reflection-property-names/

like image 23
Brandon Boone Avatar answered Sep 23 '22 17:09

Brandon Boone


You want one of the following, choose which works best for you:

Reflection: http://msdn.microsoft.com/en-us/library/136wx94f.aspx

Dynamic type: http://msdn.microsoft.com/en-us/library/dd264741.aspx

though as someone already pointed out, a class with 1000 properties is a code smell. You probably want a dictionary or collection instead,

like image 45
Neil N Avatar answered Sep 26 '22 17:09

Neil N


better version for deep search props also in basetypes

public static IEnumerable<PropertyInfo> GetAllProperties(Type t)
{
  if (t == null)
    return Enumerable.Empty<PropertyInfo>();

  BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
    return t.GetProperties(flags).Union(GetAllProperties(t.BaseType));
}
like image 22
D. Herzog Avatar answered Sep 24 '22 17:09

D. Herzog


You use reflection.

There's even an article that describes how to do what you want:-

http://geekswithblogs.net/shahed/archive/2006/11/19/97548.aspx

like image 36
Tim Almond Avatar answered Sep 23 '22 17:09

Tim Almond