From: Mr. X. on 19 Jun 2010 09:08 Hello. I need sample code of getting properties values of a specific object, please. Thanks :)
From: Armin Zingler on 19 Jun 2010 09:26 Am 19.06.2010 15:08, schrieb Mr. X.: > Hello. > I need sample code of getting properties values of a specific object, > please. I assume that you don't know the type of the object at design time, is that right? Then you're looking for reflection: http://msdn.microsoft.com/en-us/library/cxz4wk15(VS.90).aspx See sub topic "Viewing Type Information", section "MemberInfo, MethodInfo, FieldInfo, and PropertyInfo" -- Armin
From: Mr. X. on 19 Jun 2010 10:24 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 :) "Armin Zingler" <az.nospam(a)freenet.de> wrote in message news:OXJ7oM7DLHA.5724(a)TK2MSFTNGP05.phx.gbl... > Am 19.06.2010 15:08, schrieb Mr. X.: >> Hello. >> I need sample code of getting properties values of a specific object, >> please. > > I assume that you don't know the type of the object at design time, is > that right? > Then you're looking for reflection: > http://msdn.microsoft.com/en-us/library/cxz4wk15(VS.90).aspx > > See sub topic "Viewing Type Information", section "MemberInfo, MethodInfo, > FieldInfo, and PropertyInfo" > > -- > Armin
From: Armin Zingler on 19 Jun 2010 14:05 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. -- Armin
From: Mr. X. on 19 Jun 2010 16:12 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 :) "Armin Zingler" <az.nospam(a)freenet.de> wrote in message news:OgqsHo9DLHA.5428(a)TK2MSFTNGP05.phx.gbl... > 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. > > > -- > Armin
|
Pages: 1 Prev: XL app wants focus? Next: newbie help with elearning sample console.writeline |