How can i set reportViewer's Report Source Property using Reflection?

ramnujs

Well-known member
Joined
Jul 5, 2005
Messages
53
Location
Philippines
Programming Experience
3-5
I am in the process of refactoring my codes and i used late binding and usings assembly class to create my objects at runtime.

it works perfectly to Windows Forms and Custom classes created but with Reports Objects like Report viewer, i cant get the objects properties or fields or even methods... i was attempting to do the following:

object rptViewer = frmReportViewer.GetType().GetField("rptViewer");
PropertyInfo pinfo = rptViewer.GetType().GetProperty("ReportSource");

/// the pinfo returns a null value

///rtpViewer is an
CrystalDecisions.Windows.Forms.CrystalReportViewer object attached in frmReportViewer form

 
It appears your code is correct except for the ("ReportSource").
Try
(ReportSource)


Im not sure where and when you want to get and use the info, but here is a function "GetProperty" that will return ReportSource.


VB.NET:
    MessageBox.Show(GetProperty(CrystalReportViewer1, "ReportSource", "").ToString)
VB.NET:
    Function GetProperty(ByVal obj As Object, ByVal propertyName As String, _
    ByVal defaultValue As Object) As Object
        Try
            Dim pi As System.Reflection.PropertyInfo = obj.GetType().GetProperty _
                (propertyName)
            If Not pi Is Nothing Then
                Return pi.GetValue(obj, Nothing)
            End If
        Catch
        End Try

        ' if property doesn't exist or throws
        Return defaultValue
    End Function
 
It wont work still!

I have tried your function but still it doesnt work.

Let me tell you the steps that i was doing:

1. I create a frmReportViewer by reflection at runtime
2. From frmReportViewer, I dig on its field "rptViewer"
because the CrystalReportViewer object named "rptViewer" is inside in frmReportViewer using : frmReportViewer.GetType().getField("rptViewer")
the problem is the above code returns FieldInfo object.....
i am stuck at this point becoz there is no way for me to get "ReportSource" property from a FieldInfo object. if i will used your function GetProperty(), 4 sure it wil return null.
3. my ultimate goal really is to set rptViewer's ReportSource Property =some report documents.

I hope u can help me more on this.

ramnujs
 
Back
Top