This is a migrated thread and some comments may be shown as answers.

Can I get the ReportParameters via Reflection?

1 Answer 20 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Manfred
Top achievements
Rank 2
Manfred asked on 22 Jan 2013, 07:45 AM
Hi there,

currently I am working on a project to create custom reports.
I do that in this way:
I created a WCF-Service with a method which returns a Stream. This stream is the custom report, rendered to a pdf that is shown in a RadPDFViewer.
I do that because we need to send a list of all Reports that should be merged to one reportbook where every report is one single site with different data.
To create one report, I use this function:
public Report GetReport(ReportEntry entry)
{
dynamic newReport = new Object();
string parameterNameWithError = string.Empty;
string parameterValue = string.Empty;
string parameterType = string.Empty;
try
{
Type reportType = Type.GetType("ReportingService.Reports." + entry.Name);
newReport = Activator.CreateInstance(reportType);
foreach (var para in entry.ParameterList)
{
parameterNameWithError = para.Name;
parameterValue = para.Value;
parameterType = para.Type;
newReport.ReportParameters[para.Name].Value = para.Value;
}
}
catch (Exception ex)
{
newReport = new Reports.ErrorReport();
newReport.ReportParameters["ReportName"].Value = entry.Name;
newReport.ReportParameters["ParameterName"].Value = parameterNameWithError;
newReport.ReportParameters["ParameterValue"].Value = parameterValue;
newReport.ReportParameters["ParameterType"].Value = parameterType;
newReport.ReportParameters["ErrorMessage"].Value = ex.Message;
}
return newReport;
}

Now I want to create a function that gives me a list of all available reports with its ReportParameters.
I already get a list of reports, but I am now looking for a way to get the parameters.
This is what I have at the moment:

Type[] classes = Assembly.GetExecutingAssembly().GetExportedTypes();
foreach (Type t in classes)
{
  if (t.FullName.Contains("Reports.Report"))
  {
    PropertyInfo[] props = t.GetProperties();
    foreach(var prop in props)
    {
      if(prop.Name.Equals("ReportParameters"))
      {
        // What should I do here ???
      }
    }
}

Any kind of idea or hint?

Best Regards
Manfred







1 Answer, 1 is accepted

Sort by
0
Steven
Top achievements
Rank 1
answered on 25 Jan 2013, 04:15 PM
The ReportParameter Collection gets populated in the Contrustor => InitializeComponent() call, so it doesn't have any information within the class type to get with Reflection

Rather then blindly setting a parameter value that may or may not exists, just add a check in your loop.

foreach (var param in entry.ParameterList)
{
   var parameter = newReport.ReportParameters.FirstOrDefault(x => x.Name.Equals(param.Name, StringComparison.CurrentCultureIgnoreCase));
   if (parameter != null)
        parameter.Value = param.Value;
}
Tags
General Discussions
Asked by
Manfred
Top achievements
Rank 2
Answers by
Steven
Top achievements
Rank 1
Share this question
or