[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:
Type type = Browser_Wb.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(Browser_Wb));
}
And
VB.NET Code:
Type type = Browser_Wb.GetType();
BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
PropertyInfo[] properties = type.GetProperties(flags);
foreach (PropertyInfo property in properties)
{
Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(Browser_Wb, null));
}
I got error: System.ArgumentException: 'Property Get method was not found.'
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";
Re: Getting All Properties Of A Control
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:
PropertyInfo[] properties = type.GetProperties();
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#.