Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi there,

I'm trying to dynamically get all properties and their values from dynamic objects.
This is the code I have so far:


C#
//The code that should show the parameter and the parameter value
class MdlObject
{
    public void Insert(dynamic anObject)
    {
        var propertyInfos = anObject.GetType().GetProperties();
        // sort properties by name
        // write property names
        foreach (var propertyInfo in propertyInfos)
        {
            //this works perfect, returns Name and Id
            MessageBox.Show(propertyInfo.Name);
            //Gives an error, because it literally calls "anObject.propertyInfo.Name"
            MessageBox.Show(anObject.propertyInfo.Name);
            //This works, thanks Nishant Sivakumar :)
            MessageBox.Show(propertyInfo.GetValue(anObject, null).ToString());

        }
    }


C#
//Example of a simple object I want to put in
class Mdltest
{
    public String Name { get; set; }
    public int Id { get; set; }
}



C#
//Example of how to call it
var test= new Mdltest{Id = 10, Name = "just testing"};
var tmp = new MdlObject();
tmp.Insert(test);


Could someone tell me how I'm suppose to fix the "anObject.propertyInfo.Name" part? because when I use that it literally calls "anObject.propertyInfo.Name", and not for example anObject.Name and anObject.Id ...

Thanks in advance for the help :)

Gr.
Posted
Updated 13-Jul-10 9:48am
v3
Comments
Nish Nishant 13-Jul-10 15:18pm    
Reason for my vote of 5
Well phrased question. Worth a 5!

That won't work, you can do it this way:

C#
class MdlObject
{
    public void Insert(dynamic anObject)
    {
        var propertyInfos = anObject.GetType().GetProperties();
        foreach (var propertyInfo in propertyInfos)
        {
            MessageBox.Show(propertyInfo.Name);
        }

        MessageBox.Show(anObject.Name);
        MessageBox.Show(anObject.Id.ToString());
    }
}


Or do this:

C#
foreach (var propertyInfo in propertyInfos)
{
    MessageBox.Show(propertyInfo.Name);
    MessageBox.Show(propertyInfo.GetValue(anObject, null).ToString());
}
 
Share this answer
 
Comments
Necrowizard 13-Jul-10 15:37pm    
Reason for my vote of 5
Thanks, that worked :)
Necrowizard 13-Jul-10 15:40pm    
Awesome, thanks.
Yea, the first example obviously would have worked, but I was trying to keep it dynamic, so putting Name and Id there isnt really working.

However, MessageBox.Show(propertyInfo.GetValue(anObject, null).ToString()); worked, that was exactly what I was looking for :)
You need to call the GetValue method on the PropertyInfo, passing in the object you want to query.
 
Share this answer
 
Comments
Necrowizard 13-Jul-10 15:38pm    
Reason for my vote of 3
Thanks, that was the right method, not very well explained, like answer 2 tho

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900