Ive been trying to get the list of properties of control,
private void GetPropertyTree(object dependencyObject , TreeViewItem treeItem)
{
TreeViewItem currentItem = new TreeViewItem();
PropertyInfo[] properties = dependencyObject.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
TreeViewItem item = new TreeViewItem();
if (property.PropertyType.IsGenericParameter && dependencyObject.GetType().GetProperty(property.Name).GetValue(dependencyObject, null) != null)
item.Tag = dependencyObject.GetType().GetProperty(property.Name).GetValue(dependencyObject, null).GetType();
item.Header = property.Name;
if (treeItem == null)
{
propertyTreeView.Items.Add(item);
}
else
{
treeItem.Items.Add(item);
}
if (property.PropertyType.IsGenericParameter)
{
object obj = dependencyObject.GetType().GetProperty(property.Name).GetValue(dependencyObject, null);
if (obj != null)
{
Type value = obj.GetType();
// object instance = Activator.CreateInstance(property.PropertyType);
PropertyInfo[] sProps = value.GetProperties();
foreach (PropertyInfo sprop in sProps)
{
TreeViewItem sItem = new TreeViewItem();
sItem.Tag = value;
sItem.Header = sprop.Name;
item.Items.Add(sItem);
}
}
else
{
}
}
}
//throw new NotImplementedException();
}
I need to get the property's inner property also,but i've not used recursion to that,because in some cases it throws stackoverflow exception.. Here my problem is couldn't create instance of property type using Activator, it failed with no parameterless constructor method is defined exception. What can do to get the list of all possible inner properties in treeview.Please help me with your ideas. Ive basic knowledge of reflection.
Regards, kumar
Aucun commentaire:
Enregistrer un commentaire