jetbet
asked on
c# Getting Hashtable instance from PropertyInfo or FieldInfo
I am iterating through an object structure looking at values using propertyInfo and FieldInfo.
If the type is a Hashtable I want to cast to the instance and print out the key/value pairs.
I am using the following code but it does not work.
Does anyone know if this is possible
If the type is a Hashtable I want to cast to the instance and print out the key/value pairs.
I am using the following code but it does not work.
Does anyone know if this is possible
private static PropertyInfo[] GetProperties(object obj)
{
return obj.GetType().GetProperties();
}
private static FieldInfo[] GetFields(object obj)
{
return obj.GetType().GetFields();
}
private void AddDataItem(ListView listView, List<String> data)
{
if (data.Count > 0)
{
ListViewItem temp = new ListViewItem(data[0]);
for (int i = 1; i < data.Count; i++)
{
temp.SubItems.Add(data[i]);
}
listView.Items.Add(temp);
}
}
private void TV_Main_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeNode selected = TV_Main.SelectedNode;
if (selected != null)
{
LV_MainProperties.Items.Clear();
if (selected.Tag != null)
{
Object obj = selected.Tag;
var properties = GetProperties(obj);
foreach (var p in properties)
{
try
{
if (p.PropertyType == typeof(Hashtable))
{
object propVal = p.GetValue(p, null);
Hashtable h = (Hashtable)propVal;
foreach (DictionaryEntry entry in h)
{
AddDataItem(LV_MainProperties, new List<String> { entry.Key.ToString(), entry.Value.ToString() });
}
}
else
{
string name = p.Name;
var value = p.GetValue(obj, null);
AddDataItem(LV_MainProperties, new List<String> { name, value.ToString() });
}
}
catch
{
}
}
var fields = GetFields(obj);
foreach (var f in fields)
{
try
{
if (f.FieldType == typeof(Hashtable))
{
object propVal = f.GetValue(f);
Hashtable h = (Hashtable)propVal;
foreach (DictionaryEntry entry in h)
{
AddDataItem(LV_MainProperties, new List<String> { entry.Key.ToString(), entry.Value.ToString() });
}
}
else
{
string name = f.Name;
var value = f.GetValue(obj);
AddDataItem(LV_MainProperties, new List<String> { name, value.ToString() });
}
}
catch
{
}
}
}
}
}
On the first glance I'd say:
if (p.ReflectedType == typeof(Hashtable))
{
Hashtable ht = (Hashtable)p;
//do whatever you need with this hashtable...
ASKER
I would have thought that too, but I get an error 'Cannot convert type 'System.Reflection.Propert yInfo' to 'System.Collections.Hashta ble'
Same thing for FieldInfo
Same thing for FieldInfo
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No luck for me so far.
Even though the Hashtable is a field
The code only triggers as a property
The test of p.PropetyType fails as it returns a collection not a Hashtable.
The test p.ReflectedType does return a Hashtable but then the cast
fails with a parameter count mismatch
Even though the Hashtable is a field
public class RunnerDetail
{
...
Hashtable _winTable = new Hashtable();
...
public Hashtable winTable
{
get { return _winTable; }
set { _winTable = value; }
}
...
The code only triggers as a property
PropertyInfo[] props = obj.GetType().GetProperties();
foreach (var p in props)
{
try
{
if (p.PropertyType == typeof(Hashtable))
{
String message = "bob";
}
else if (p.ReflectedType == typeof(Hashtable))
{
Hashtable ht = (Hashtable)p.GetValue(obj);
foreach (DictionaryEntry entry in ht)
{
AddDataItem(LV_MainProperties, new List<String> { entry.Key.ToString(), entry.Value.ToString() });
}
}
else
{
string name = p.Name;
var value = p.GetValue(obj);
AddDataItem(LV_MainProperties, new List<String> { name, value.ToString() });
}
}
catch (Exception ex)
{
String message = ex.Message;
}
}
The test of p.PropetyType fails as it returns a collection not a Hashtable.
The test p.ReflectedType does return a Hashtable but then the cast
Hashtable ht = (Hashtable)p.GetValue(obj);
fails with a parameter count mismatch
What type of object is "obj" in your code? Is it hashtable?
PropertyInfo[] props =[b] obj.[/b]GetType().GetProperties();
ASKER
TreeNode selected = TV_Main.SelectedNode;
if (selected != null)
{
LV_MainProperties.Items.Clear();
if (selected.Tag != null)
{
Object obj = selected.Tag;
So it is an instance of the class (RunnerDetail) I created.
The problem is - I cannot understand the relation between your 'obj' and the hashtable. The 'tag' property has a type of 'Object'! So the code
Object obj = selected.Tag;
tells me nothing.
What is the relation between Tag and hashtable in this case?
Object obj = selected.Tag;
tells me nothing.
What is the relation between Tag and hashtable in this case?
ASKER
The Tag holds a reference to the instance of the object that the hashtable is part of.
The code lists all the names and values of the properties and fields on the object.
This works fine and will list that there is a hashtable there.
What I am trying to do is to drill down into the hashtable to show the values of the entries.
The code lists all the names and values of the properties and fields on the object.
This works fine and will list that there is a hashtable there.
What I am trying to do is to drill down into the hashtable to show the values of the entries.
OK, I give up.
I showed you on my example how to retrieve the hashtable which is a property of the object. But I can't help without knowing details.
You tell
ReflectedType in your case should be your Tag object, not it's property.
I'd recommend to read MSDN documentation.
http://msdn.microsoft.com/ en-us/libr ary/system .reflectio n.memberin fo.reflect edtype(v=v s.110).asp x
I showed you on my example how to retrieve the hashtable which is a property of the object. But I can't help without knowing details.
You tell
The test p.ReflectedType does return a Hashtable but then the castbut
ReflectedType in your case should be your Tag object, not it's property.
I'd recommend to read MSDN documentation.
http://msdn.microsoft.com/
ASKER
OK
Sorry if I have muddied the waters for you. I am unsure what details I was missing.
Thanks for all your help, I appreciate your efforts.
Sorry if I have muddied the waters for you. I am unsure what details I was missing.
Thanks for all your help, I appreciate your efforts.
ASKER
This was the correct solution. The following questions we not correct as I was passing an incorrect object into the Tag property.
ASKER
If I use
Open in new window
Then I do find the Type but propVal is the Hashtable class not the instance.