|
-
Jun 23rd, 2018, 02:18 PM
#1
Thread Starter
Addicted Member
[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.'
I'm not a man of too many faces
The mask I wear is one
-
Jun 23rd, 2018, 02:37 PM
#2
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";
-
Jun 23rd, 2018, 03:30 PM
#3
Thread Starter
Addicted Member
Re: Getting All Properties Of A Control
I'm not a man of too many faces
The mask I wear is one
-
Jun 23rd, 2018, 09:18 PM
#4
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#.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|