Reputation: 4803
i have a class that is loaded dynamically and i don't know in advance how many (or of what type) properties it contains. i would like to load all its properties into a PropertyGrid in a way that it would display as it displays a regular class.
for instance, if this is my class:
class TestPropertyObject
{
[Category("Names")]
[Description("Enter your name")]
public string Name { get; set; }
[Category("Numbers")]
[Description("Enter your number")]
public int Number { get; set; }
}
and i use reflection to generate an object and get its properties:
PropertyInfo[] info = obj.GetType().GetProperties()
how do i display the properties in the PropertyGrid?
i already ready many times this article
http://www.codeproject.com/Articles/4448/Customized-display-of-collection-data-in-a-Propert
but i just can't seem to get it to work.
some guidance would be very much appreciated.
thanks!
Upvotes: 0
Views: 722
Reputation: 2379
here is a solution, you just need the instance of the class.
var OptionsPropertyGrid = new PropertyGrid();
OptionsPropertyGrid.Size = new Size(300, 250);
this.Controls.Add(OptionsPropertyGrid);
TestPropertyObject appset = new TestPropertyObject();
OptionsPropertyGrid.SelectedObject = appset;
This sample is assuming that you class's property is decorated with Category/description. let me know if I have misunderstood it.
Or by using dynamic object
var OptionsPropertyGrid = new PropertyGrid();
OptionsPropertyGrid.Size = new Size(300, 250);
this.Controls.Add(OptionsPropertyGrid);
this.Text = "Options Dialog";
string classname = "WindowsFormsApplication1.TestPropertyObject";
var type1 = Type.GetType(classname);
object obj = Activator.CreateInstance(type1);
OptionsPropertyGrid.SelectedObject = obj;
Make sure you can be able get the namespace of the class.
Upvotes: 1