Avatar of jetbet
jetbet
Flag for New Zealand 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
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
                        {

                        }
                    }
                }
            }  

        }

Open in new window

C#.NET Programming

Avatar of undefined
Last Comment
jetbet

8/22/2022 - Mon
jetbet

ASKER
Update:

If I use
if (p.ReflectedType == typeof(Hashtable))
    {
     object propVal = p.ReflectedType;  
.....

Open in new window

Then I do find the Type but propVal is the Hashtable class not the instance.
Dmitry G

On the first glance I'd say:

if (p.ReflectedType == typeof(Hashtable))
    {
            Hashtable ht = (Hashtable)p;
             //do whatever you need with this hashtable...

Open in new window

jetbet

ASKER
I would have thought that too, but I get an error 'Cannot convert type 'System.Reflection.PropertyInfo' to 'System.Collections.Hashtable'

Same thing for FieldInfo
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Dmitry G

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jetbet

ASKER
No luck for me so far.
Even though the Hashtable is a field
public class RunnerDetail
    {
...
Hashtable       _winTable = new Hashtable();
...
public Hashtable winTable
        {
            get { return _winTable; }
            set { _winTable = value; }
        }
...

Open in new window


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;
                        }
                    }

Open in new window


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);

Open in new window


fails with a parameter count mismatch
Dmitry G

What type of object is "obj" in your code? Is it hashtable?

PropertyInfo[] props =[b] obj.[/b]GetType().GetProperties();

Open in new window

jetbet

ASKER
TreeNode selected = TV_Main.SelectedNode;
            if (selected != null)
            {
                LV_MainProperties.Items.Clear();
                if (selected.Tag != null)
                {
                    Object obj = selected.Tag;

Open in new window


So it is an instance of the class (RunnerDetail) I created.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Dmitry G

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?
jetbet

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.
Dmitry G

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
The test p.ReflectedType does return a Hashtable but then the cast
but
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/library/system.reflection.memberinfo.reflectedtype(v=vs.110).aspx
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
jetbet

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.
jetbet

ASKER
This was the correct solution. The following questions we not correct as I was passing an incorrect object into the Tag property.