using Microsoft.Reporting.WebForms; using System; using System.Collections; using System.Collections.Generic; using System.Reflection; namespace Vrh.Web.OneReport.ReportViewerForMvc { internal static class ReportViewerHelper { internal static ReportViewer AnonymousToReportViewer(object obj) { try { return obj.ToType(); } catch (ArgumentException ex) { throw new ArgumentException("Could not convert anonymous object to type ReportViewer", ex); } } internal static LocalReport AnonymousToLocalReport(object obj) { try { return obj.ToType(); } catch (ArgumentException ex) { throw new ArgumentException("Could not convert anonymous object to type LocalReport", ex); } } internal static ServerReport AnonymousToServerReport(object obj) { try { return obj.ToType(); } catch (ArgumentException ex) { throw new ArgumentException("Could not convert anonymous object to type ServerReport", ex); } } internal static List AnonymousToReportParameter(object obj) { List reportParameters = new List(); foreach (KeyValuePair keyValuePair in obj.ToDictionary()) { reportParameters.Add(new ReportParameter(keyValuePair.Key, keyValuePair.Value)); } return reportParameters; } internal static List AnonymousToReportDataSourceList(object obj) { List reportDataSourceList = new List(); try { if (obj.GetType().IsArray) { foreach (var reportDataSource in (IEnumerable)obj) { reportDataSourceList.Add(reportDataSource.ToType()); } } else { reportDataSourceList.Add(obj.ToType()); } } catch (ArgumentException ex) { throw new ArgumentException("Could not convert anonymous object to type ReportDataSource", ex); } return reportDataSourceList; } private static T ToType(this object obj) { if (obj == null) { throw new ArgumentNullException("obj", "Value cannot be null."); } T instance = Activator.CreateInstance(); foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties()) { var property = typeof(T).GetProperty(propertyInfo.Name); if (property == null) { throw new ArgumentException("An attempt was made to set the property '" + propertyInfo.Name + "' that is not found on object type '" + typeof(T).Name + "'"); } property.SetValue(instance, propertyInfo.GetValue(obj)); } return instance; } private static IDictionary ToDictionary(this object obj) { IDictionary dic = new Dictionary(); foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties()) { dic.Add(propertyInfo.Name, propertyInfo.GetValue(obj).ToString()); } return dic; } } }