Share via


casting from object to System.Reflection.PropertyInfo

Question

Friday, June 17, 2011 9:15 AM

Hi i have the below code in C#

private void AssignHashTable(ref CompanyRow companyRow, ref Hashtable arrColData, string[] colData)
        {
            int index = 0;
            Type type = companyRow.GetType();
            PropertyInfo p = default(PropertyInfo);
            PropertyInfo[] props = type.GetProperties();
            try
            {
                for (index = 0; index <= props.Length - 1; index++)
                {
                    p = props.GetValue(index);
                    arrColData.Add(p.Name, colData[index]);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Debatcher - AssignHashTable", ex.Message + "\n" + ex.StackTrace, System.Diagnostics.EventLogEntryType.Error);
            }
        }

 

it ws showing an error when i build

    Cannot implicitly convert type 'object' to 'System.Reflection.PropertyInfo'. An explicit conversion exists (are you missing a cast?)   

 

how to do casting for this kind of error

waiting for reply

thanks

madhusudhan

All replies (1)

Friday, June 17, 2011 9:27 AM âś…Answered

p = props.GetValue(index);

The GetValue method here return a Object which cannot be implicitly converted to a PropertyInfo Type. So you need to explicitly convert it to PropertyInfo like this

p = (PropertyInfo)props.GetValue(index);

 

Regards, Krishnakant This answers, please mark as answered, if this helps please mark as helpful.