dcsimg
Results 1 to 4 of 4
  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Resolved [RESOLVED] Getting All Properties Of A Control

    Hi I have a webbrowser on my form and having problem with getting its values. I followed the page Question about How to loop through all the properties of a class

    VB.NET Code:
    1. Type type = Browser_Wb.GetType();
    2.  
    3. PropertyInfo[] properties = type.GetProperties();
    4.  
    5. foreach (PropertyInfo property in properties)
    6. {
    7.     Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(Browser_Wb));
    8. }

    And

    VB.NET Code:
    1. Type type = Browser_Wb.GetType();
    2.  
    3. BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
    4. PropertyInfo[] properties = type.GetProperties(flags);
    5.  
    6. foreach (PropertyInfo property in properties)
    7. {
    8.     Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(Browser_Wb, null));
    9. }

    I got error: System.ArgumentException: 'Property Get method was not found.'
    I'm not a man of too many faces
    The mask I wear is one

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,933

    Re: Getting All Properties Of A Control

    Not all properties are read and write, some are read-only (no set), and some are write-only (no get). It seems you have found one that is write-only.

    To avoid the error for that property (and any others that are write-only), you can apparently check if the get is available (using property.GetGetMethod()), which you should do before trying to call it, eg:
    Code:
        if (property.GetGetMethod() != null)
            Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(Browser_Wb, null));
        else
            Console.WriteLine("Name: " + property.Name + ", value is write-only";

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Getting All Properties Of A Control

    Thank that helped a lot.
    I'm not a man of too many faces
    The mask I wear is one

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,144

    Re: [RESOLVED] Getting All Properties Of A Control

    If you want to ignore the write-only properties, you could include the check for getter in the loop statement:
    csharp Code:
    1. PropertyInfo[] properties = type.GetProperties();
    2.  
    3. foreach (PropertyInfo property in properties.Where(pi => pi.GetMethod() != null))
    By the way, note that I used "csharp" as the option for the HIGHLIGHT tag to get syntax-highlighting appropriate to C#.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width