Using reflaction - properties that are shown on PropertyGrid and can be writeable.

M

Mr. X.

Hello,
For the following code,
I had a problem.
I want to search only the properties that can be shown and writeable outside
(to a file, etc ...)
=====================

public findWritable(Control myControl)
{
System.Reflection.PropertyInfo[] prs = null;
bool foundBA;

prs =
MyControl.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foundBA = false;
for (int j = 0; j <= prs.Count() - 1; j++)
{
foundBA = true;
if
(prs[j].GetCustomAttributes(false).Count() == 0)
{
foundBA = false;
}
else
{
foundBA = true;
foreach (object att_loopVariable in
prs[j].GetCustomAttributes(false))
{
if (att_loopVariable is
System.ComponentModel.BrowsableAttribute)
{
System.ComponentModel.BrowsableAttribute
att;
att =
(System.ComponentModel.BrowsableAttribute)att_loopVariable;
if (!(att as
System.ComponentModel.BrowsableAttribute).Browsable)
{
foundBA = false;
}
}
}
}
if (foundBA & prs[j].CanRead &&
prs[j].CanWrite)
{
// Property is visible and can be read neighter write
// *** ... DO SOMETHING ... ***//
}
}
}
======
The above code doesn't work for TextBox.Text
and also has some extra elements for TextBox (AutoCompleteCustomResource).

(//*** Do something ***// in remarks should work fine, with no problems).

What's wrong with my code?
I need a generic solution, please.

Thanks :)
 
M

Mr. X.

What I want is to see exactly the properties that are visible on Visual
Studio IDE
(If I through a TextBox on the form, I can see the list of properties :
How can I get the list of properties, the same way I see on IDE (one by
one) ? )

Thanks :)

Peter Duniho said:
Mr. X. said:
Hello,
For the following code,
I had a problem.
I want to search only the properties that can be shown and writeable
outside (to a file, etc ...)
[...]
The above code doesn't work for TextBox.Text
and also has some extra elements for TextBox
(AutoCompleteCustomResource).

Define "doesn't work". What output did you expect and what output did you
get instead?

I see a number of problems with the code, some of them simply related to
inefficiency or maintainability, but others that appear contrary to what
someone might normally want to do.

But without knowing exactly what it is you're _trying_ to do, and what
you're getting instead, it's hard to say for sure what's important to fix.

In the meantime, I'll offer this broad suggestion: the code you posted
appears to maintain a flag detecting whether the BrowsableAttribute is
found or not, and ignores properties for which that attribute is not
attached. Since that attribute is generally used to _suppress_ browsing,
it is often not found on properties that are in fact browseable.

You may find that if you conceptualize the problem not as "did I find the
BrowsableAttribute attribute", but instead as "is this property
browsable?" and keep in mind that without the BrowsableAttribute present,
the default is for a public property to be browsable, that may help you
rewrite the loop so that it works more to your liking.

As for the other properties you discover, obviously they are making it
through the filtering you're trying to do. Perhaps once you've fixed the
filtering so that it actually does what you want, that issue will go away
too. :)

Pete
 
Top