Get property values from object.

  • Thread starter Thread starter Mr. X.
  • Start date Start date
M

Mr. X.

Hello.
I need sample code of getting properties values of a specific object,
please.

Thanks :)
 
Indeed, I am using reflection,
but for the following code, I am doing a loop, that doesn't really work :

dim c as control
dim I ,j as integer ...
Dim prs() As System.Reflection.PropertyInfo
dim propName as string
dim propValue

For i = 0 To myPanel.Controls.Count - 1
c = myPanel.controls(I) ' one of the child controls is a dataGridView
prs = c.getType().getProperties()
for j = 0 to prs.count - 1
...
' I am looking for the published properties only.
if prs(j).canRead and prs(j).canWrite then
' for the datagridview, I am getting the propety
propName = prs(j).name
propValue = prs(j).getValue(c, nothing) ' *** here I get sometimes an
exception : TargetParameterCountException was unhandled.
parameterCountMismatch

....

Why may be the reason for the exception (The control : DataGridView.
property : Item) ?
Also, "item" is not a published property, that may seen on design time.
I want to see only properties, which are seen on design time.

Thanks :)
 
Am 19.06.2010 16:24, schrieb Mr. X.:
Indeed, I am using reflection,
but for the following code, I am doing a loop, that doesn't really work :

dim c as control
dim I ,j as integer ...
Dim prs() As System.Reflection.PropertyInfo
dim propName as string
dim propValue

For i = 0 To myPanel.Controls.Count - 1
c = myPanel.controls(I) ' one of the child controls is a dataGridView
prs = c.getType().getProperties()
for j = 0 to prs.count - 1
...
' I am looking for the published properties only.
if prs(j).canRead and prs(j).canWrite then
' for the datagridview, I am getting the propety
propName = prs(j).name
propValue = prs(j).getValue(c, nothing) ' *** here I get sometimes an
exception : TargetParameterCountException was unhandled.
parameterCountMismatch

....

Why may be the reason for the exception (The control : DataGridView.
property : Item) ?

TargetParameterCountException means that the count of the passed parameters
are incorrect. The 'Item' property requires two parameters as you can see
via intellisense, in the object browser and in the documentation. The call
would be:

value = prs(j).GetValue(c, New Object() {param1, param2})

You'd have to pass the correct parameter types. You get the parameter information
by calling

dim pi as parameterinfo
pi = prs(j).GetIndexParameters(ParameterIndex)

However, if you don't know what to pass to the GetValue method, it's not possible
to get the property value.
Also, "item" is not a published property, that may seen on design time.
I want to see only properties, which are seen on design time.

Item is a Public property. You may want to check the custom attributes of
each property. Call prs(j).GetCustomAttributes. Then you can check the type
of each attribute, for example.

dim prop = prs(j)
dim atts = prop.GetCustomAttributes

for each att in atts
if typeof att is System.ComponentModel.DesignerSerializationVisibilityAttribute then
dim SpecialAtt = directcast(att, System.ComponentModel.DesignerSerializationVisibilityAttribute)
debug.print (specialatt.visibility.tostring)
end if
next

The DesignerSerializationVisibilityAttribute is one of the attributes attached to the DataGridView's
Item property. The other one is the System.ComponentModel.BrowsableAttribute.

Anyway, even those properties that don't have any of these attributes attached, may have multiple
parameters. If you don't know what to pass, you can only retrieve the values of those that's
GetIndexParameters method returns an empty array, i.e. with 0 items.
 
Great ... Thanks :)

I only need to search all the properties of my control (only properties
which can be shown on the designer).
As your advise, I did getProperties & getCustomAttributes either :
prs = ... getProperties
for I = 0 to prs.count - 1
dim prop = prs(j)
dim atts = prop.GetCustomAttributes
for each att in atts ... ' as your code below.

.... but I have reach the inner loop only when :specialAtt.Visibility was
hidden.

Thanks :)
 
Back
Top