Reflection, objects, and comparing types

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mattb123
    New Member
    • May 2010
    • 8

    #1

    Reflection, objects, and comparing types

    Hi, I'm a vb.net developer getting the hang of c# and I've gotten stuck on trying to compare types. In the following block, I'm populating an object from a DataTable. The table's data types aren't always the right type so as I loop through I want to compare types and optionally try to convert to make the data match the object property types.

    Code:
                Type t = _Item.GetType();
    
                foreach (DataRow r in dtIT.Rows)
                    {
                        foreach (PropertyInfo p in t.GetProperties())
                        { 
                            //assign any matching column names to object properties
                            if (dtIT.Columns[p.Name] != null)
                            {
                               if (!(r[p.Name].GetType is p.GetType))
                                {
                                    //convert type if possible
                                }
    
                               p.SetValue(_Item, r[p.Name], null);
                            }
                        }
    what happens in the above block is I can't even build and Intellisense is flagging the p in p.GetType with this message:

    Error 1 The type or namespace name 'p' could not be found (are you missing a using directive or an assembly reference?) ...\Classes\Ite mTreeData.cs 61 55 DAL

    Can anyone tell me what I'm doing wrong or have any suggestions for me? Thanks!
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    try it without the "is"... I *think* 'is' is only VB. Instead use the 'is equal to' double equal sign

    Code:
    if (!(r[p.Name].GetType == p.GetType))

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      typeof(p)
      ?

      Comment

      • mattb123
        New Member
        • May 2010
        • 8

        #4
        Wow, you guys are quick! Thanks for the replies.

        Code:
        if (r[p.Name].GetType == p.GetType)
        Results in the following error in the IDE (won't build):
        Error 1 Operator '==' cannot be applied to operands of type 'method group' and 'method group' C:\Users\mburt\ Documents\Visua l Studio 2010\Projects\e Commerce\EComm_ DAL\Classes\Ite mTreeData.cs 61 33 DAL

        Code:
         if (r[p.Name].GetType == typeof(p))
        In the above the p gets the red underline from Intellisense with the following error:
        Error 1 The type or namespace name 'p' could not be found (are you missing a using directive or an assembly reference?) C:\Users\mburt\ Documents\Visua l Studio 2010\Projects\e Commerce\EComm_ DAL\Classes\Ite mTreeData.cs 61 61 DAL

        Which seems odd to me because I use p.name without incident a few lines above.

        Thanks again!
        Matt

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          error: cannot be applied to operands of type 'method group' and 'method group'
          Means you are calling a method without the parenthises for the paramaters. Sorry I should have seen it earlier but I've been up all night.

          .GetType is a method and requires .GetType()

          Code:
          if (r[p.Name].GetType() == p.GetType())

          Comment

          • mattb123
            New Member
            • May 2010
            • 8

            #6
            Looks like that was it! Thank you SO MUCH!

            Comment

            Working...