0

Given the following class:

public class SampleViewModel
{
    public TheModel Model { get; set; }

    public SampleViewModel()
    {
        this.Model = new TheModel();
    }

    public class TheModel
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime Birthdate { get; set; }
    }
}

Which is instantiated as:

var svm = new SampleViewModel();

How do I, with reflection, list the properties within svm.Model?

Note: If I use the normal GetProperties method on svm.Model I get 14 properties, all kinds of propertyinfo fields popup and none of the properties from TheModel class.

Note 2: Ok, tried with an external code like this:

 var svm = new SampleViewModel();
 var props = typeof(SampleViewModel).GetProperties();
 var innerprops = svm.Model.GetType().GetProperties();

This seems to work, now I need to figure out why the same doesnt't work when I do it inside my framework with instances created with Activator.CreateInstance. :)

thanks, pom

4

2 回答 2

1

Getting the properties of the property Model in SampleViewModel, you need to call:

typeof(SampleViewModel.TheModel).GetProperties();

or

svm.Model.GetType().GetProperties();
于 2013-10-24T10:18:31.333 回答
1
PropertyInfo[] properties = svm.Model.GetType().GetProperties();
        foreach (PropertyInfo p in properties) 
            Console.WriteLine(p.Name);

Output

Name
Age
Birthdate

GetProperties() work correctly also for Model as you see

于 2013-10-24T10:19:33.560 回答