Coder Perfect

Get information about an unknown object’s attributes and values.


I’ve chosen to branch out from PHP and try C#. I’ve looked everywhere but can’t seem to find the solution to how to accomplish anything similar.

$object = new Object();

$vars = get_class_vars(get_class($object));

foreach($vars as $var)

I’m working with a List of an item. The object will have a set of public attributes and could be one of three sorts. I’d like to be able to acquire a list of the object’s properties, loop through them, and then write them to a file. I believe it has something to do with c# reflection, but I’m not sure.

I’d be grateful for any assistance.

Asked by m4rc

Solution #1

That ought to suffice:

Type myType = myObject.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());

foreach (PropertyInfo prop in props)
    object propValue = prop.GetValue(myObject, null);

    // Do something with propValue

Answered by Cocowalla

Solution #2

void Test(){
    var obj = new{a="aaa", b="bbb"};

    var val_a = obj.GetValObjDy("a"); //="aaa"
    var val_b = obj.GetValObjDy("b"); //="bbb"
//create in a static class
static public object GetValObjDy(this object obj, string propertyName)
     return obj.GetType().GetProperty(propertyName).GetValue(obj, null);

Answered by Vo Van Khoa

Solution #3

Yes, contemplation is the way to proceed. First, you’d receive the Type, which indicates the type of the instance in the list (at runtime). You can achieve this by using Object’s GetType function. Because it’s on the Object class, it can be called by any object in.NET, as all types derive from Object (well, technically, not all, but that’s beside the point).

After you’ve obtained the Type instance, use the GetProperties method to obtain PropertyInfo instances, which represent run-time information about the Type’s properties.

You can use GetProperties’ overloads to assist classify which properties you retrieve.

After that, you’d just write the information to a file.

Translating your code above, you’d get:

// The instance, it can be of any type.
object o = <some object>;

// Get the type.
Type type = o.GetType();

// Get all public instance properties.
// Use the override if you want to classify
// which properties to return.
foreach (PropertyInfo info in type.GetProperties())
    // Do something with the property info.

Note that if you want information on methods or fields, you must use one of the overloads of the GetMethods or GetFields methods, respectively.

It’s also worth noting that listing all the members to a file is one thing, but you shouldn’t utilize this data to drive reasoning based on property sets.

If you have control over the kinds’ implementations, you should derive from a common base class or implement a common interface and make calls to it (you may use the as or is operator to figure out which base class/interface you’re working with at runtime).

However, if you don’t have authority over these type definitions and must rely on pattern matching to drive logic, that’s acceptable.

Answered by casperOne

Solution #4

In C#, it’s a similar situation. Here’s a simple example (only applicable to public properties):

var someObject = new { .../*properties*/... };
var propertyInfos = someObject.GetType().GetProperties();
foreach (PropertyInfo pInfo in propertyInfos)
    string propertyName = pInfo.Name; //gets the name of the property

Answered by Alex

Solution #5

Linq solution in a single line…

var obj = new {Property1 = 1, Property2 = 2};
var property1 = obj.GetType().GetProperties().First(o => o.Name == "Property1").GetValue(obj , null);

Answered by Chris Wu

Post is based on