InvalidCastException (Failed to create custom field 'C1.C1Report.CustomFields.Chart' from assembly 'c1.c1report.customfields.4')
This issue is observed as the CustomFields dll and the report definition file is generated with a specific version of C1WpfReport. This blog is to help you to resolve the error. Here are few specific steps that you need to take to make this work. Let me elaborate them in detail.
Step 1 : Regenerate C1.WPF.C1Report.CustomFields.dll
Open the WPF CustomFields sample in "....Documents\ComponentOne Samples\Studio for WPF\C1.WPF.Report\CS\CustomFields". This sample is built on .NET framework 3.0, so change its Target framework to 4.0. After this remove all the C1 references from the
project. Also, remove the PresentatioCore.dll and add PresentationCore.dll compatible with .NET framework 4.0. You would find that under.NET tab in the "Add References.." window.
Add following .NET framework 4.0 compatible C1 dll's available in "C:\Program Files\ComponentOne\Studio for WPF\C1WPFReport\Designer.4" folder :
Modify the licenses file and replace the contents with the following line :
Rebuild this sample in order to generate C1.WPF.C1Report.CustomFields.dll. Now add the generated dll to your project.
Step 2 : Add references to your application
Open your application and add reference to the following dlls in your project :
- C1.WPF.C1Report.4.dll (from "C:\Program Files\ComponentOne\Studio for WPF\bin\v4" folder")
- C1.WPF.C1Report.CustomFields.dll (created in Step 1)
- C1.Win.C1Chart.4.dll (from "C:\Program Files\ComponentOne\Studio for WPF\C1WPFReport\Designer.4" folder)
Step 3 : Modify report definition file
The xml report definitions that you are loading on the should be created with the same designer version. If not, you can change edit the xml definition. Generally,xml report definitions start with following text:-
You also need to edit the xml report definition with the assembly to be used in rendering the custom fields. So if you open up any report definition, you would notice following text in field definition:-
So, the modified xml text would be:-
Now you can load the report definition files in your WPF application without any problem. I am attaching a sample implementing the above steps with C1WpfReport build 4.6.20112.54408.