Setting Properties with System.Reflection

  • Thread starter Thread starter Lucas Tam
  • Start date Start date
L

Lucas Tam

Hi all,

I need to set a property on a subclass such as:

Report.ExportOptions.ExportFormatType = ExportFormatType.Excel

AND


Report.ExportOptions.FormatOptions = excelFormatOpts


How would I go about doing this? Do I use the Invoke Member Method?

Thanks.
 
Yes - you could call the Invoke on the Set method for the property or you
could directly call the SetValue method that's off the PropertyInfo object
which you can retrieve from the type object of your class. There's atleast
one example of SetValue in MSDN which should do what you want.

hope that helps..
Imran.
 
Yes - you could call the Invoke on the Set method for the property or
you could directly call the SetValue method that's off the
PropertyInfo object which you can retrieve from the type object of
your class. There's atleast one example of SetValue in MSDN which
should do what you want.


Yes, but how do I access the subclass properties?

I tried:

Dim pi As System.Reflection.PropertyInfo = objReport.GetType.GetProperty
("ExportFormatOptions")

Which returns the ExportFormatOptions object but I have no access to:
Report.ExportFormatOptions.FormatType or
Report.ExportFormatOptions.ExportOptions


So I tried objReport.GetType.GetProperty
("ExportFormatOptions.FormatType") but that didn't work.

Any ideas?
 
Get the type object of the subclass (from the instance of the subclass thats
returned via the property ExportFormatOptions) and then get the propertyinfo
object corresponding to its property.

dim pi as system.reflection.propertyinfo = _
objReport.ExportFormatOptions.GetType.GetProperty("FormatType")

pi.SetValue(objReport.ExportFormatOptions, ExportFormatType.Excel, Nothing)

hope that helps..
Imran.
 
Get the type object of the subclass (from the instance of the subclass
thats returned via the property ExportFormatOptions) and then get the
propertyinfo object corresponding to its property.

Ahhh.. thank you. Your example cleared things up. My code is working good
now.

Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top