Reporting with data source containing derived types

I have a situation where I have two classes, one base (class A) and one derived (class B):

 public class A     // base class
 {
      public int foo { get; set; }
 }

 public class B : A   // B is derived from A
 {
      public int bar { get; set; }
 }

I also have a class C which is the data source I pass to BuildReport() and it has a property which is a reference to the base class, A.

 public class C     // data source with reference to base class
 {
      public A baz { get; set; }
 }

I then create an instance of class C with a reference to the derived class B as follows:

 var datasource = new C();
 datasouce.baz = new B();
 BuildReport(doc, datasource, "data");

In my template markup, I find that I cannot reference a property (i.e., bar) that is on the derived class, even though the reference in the data set is to an instance of the derived class: <<[data.baz.bar]>> does not work. It seems I can only reference properties that are on the base class (i.e. foo) since that is the type of the reference.

My question is whether this limitation is true or not, or did I miss something.

Eventually, I figured out how to use KnownTypes and perform a cast in the markup, but it was a lot of extra work to make that happen and made my code more fragile.

Of course what I really would like is for you to support using C# dynamic and expando objects as data sources in BuildReport, but I’ve already requested that (as have others)… hopefully it is on your roadmap :smiley:

Thanks,

Fred

@FredFischer

Thanks for your inquiry. As per my understanding in your scenario, you are able to resolve issue using ReportingEngine.KnownTypes.

In reference to your requirements, we will appreciate it if you please share your Word template along with a standalone console application (source code without compilation errors). We will look into it and will log a feature request accordingly.

Thanks Tilal, I appreciate your answer.

Can you tell me if the limitation I identified in my post (can’t reference properties of a derived class in template) is the standard behavior of Aspose.Words 17.9?

Thanks,

Fred

@FredFischer,

Thanks for your feedback. In general, it should not be the issue to refer derived class properties in template. However as requested above please share your template along with a standalone console application (source code without compilation errors). We will look into it and will provide you information accordingly.

Hi Tilal,

I have attached a small sample console application that demonstrates the problem accessing properties of a derived type through a reference to the base type. The BuildReport() method throws an exception when trying to access the “F2” field even though it does exist on the (derived) object referenced in the data source.

aspose_derived_class.zip (146.3 KB)

@FredFischer

Thanks for sharing the sample project. After initial investigation, we have logged a ticket WORDSNET-16018 in our issue tracking system for further investigation and resolution. We will keep you updated about the issue resolution progress within this forum thread.

@FredFischer

Thanks for your patience. We have investigated the issue and found it is not a bug. Here is a relevant quote from LINQ Reporting Engine documentation:

While composing expressions, you can use a subset of C# language that satisfies C# Language Specification 5.0.

Thus, if an expression is not permissible by the C# compiler, it is not permissible by LINQ Reporting Engine as well. In your code access to ds.Object.F1 is permissible by the C# compiler but not to ds.Object.F2. LINQ Reporting Engine behaves identically. So this is an expected behavior rather than a bug.

To access Derived.F2 in this case, you may use typecasting, ((Derived)ds.Object).F2 and typeof(Derived) should be added to ReportingEngine.KnownTypes. As per your above reply, it seems you have already found the solution.

Thanks Tilal. That explanation makes sense.