Of course, you can use an object of any custom type (without violating encapsulation) as a data source or for invocation of its static members. See Accessing Type Members for details.
Thank you, I have tried the following:
public static class TemplateSupport
{
public static int Test { get; set; } = 99;
public static string Lookup()
{
return "Helloer";
…
var engine = new ReportingEngine();
engine.KnownTypes.Add(typeof(Helper));
engine.KnownTypes.Add(typeof(TemplateSupport));
But in the template I have the following but fails:
Doh, yes , thanks, I will get my eyes checked
How do I pass data to the method, for example, I would like to do the following:
<<[TemplateSupport.Lookup(GeneralInfo)]>>
Code:
I can see the data is being passed if the param is an Object, but what is the best way to access the data?
What does GeneralInfo
represent? An element of a JSON object? Or an object of a regular type? Or something else, maybe?
This is a JSON object via JsonDataSource
Not all JSON elements can be casted to DataRow
. However, if such a typecast works in your case, then you can use DataRow
as a parameter type for TemplateSupport.Lookup
.
Then, most likely, data as DataRow
would return null in case when TemplateSupport.Lookup
’s parameter type is object, would not it?
But you can also see that DataRow as a param causes errors in the template.
The reason for this is the same. As I shared earlier:
There is no typecast to DataRow
for this JSON element. That is why, neither the typecast works, nor the method is resolved.
Casting JSON elements to DataRow
is related to details of JsonDataSource
’s internal implementation. Therefore, this is not a recommended technique at all.
This requirement can be fullfilled in another way:
-
For every expression tag that is in a false-branch of an
if
tag, simply keep it as is. -
For every expression tag that is in a true-branch of an
if
tag, wrap the expression
like this:<<[Util.Ensure(original_expression)]>>
. -
Implement the
Util
class in your code as follows:
public static class Util
{
public static object Ensure(object value)
{
if (value == null) // The value is missing, throw.
throw ...
return value;
}
}
- Make the engine aware of the
Util
class as described at Setting up Known External Types.
I thought this would work but this only works for a single element, this will fail if I try to use GeneralInfo.Missing
,
You are passing System.Data.DataRow
:
I am afraid, from the description, it is not clear enough what the problem is. You can process values of different types differently providing special handling of DataRow
, if needed.
I cannot cast value to System.Data.DataRow, even though as you can see the object is System.Data.DataRow. I believe this is because you have obfuscated your code ({mK}).
Typically, a debugger window shows all data fields (including private ones) of an object. What it shows here in particular is that an object being viewed has a reference to a DataRow
instance, but this does not mean that the object itself can be casted to DataRow
. As I mentioned previously:
This is the case, where the typecast cannot be performed.
So would one access the data that is passed as a JSON object?
I have the simuilar problem with the following table:
Bolth tables use the same DTO, however if zero rows are return then we get the following error:
In this use case, we are using a strongly type mode:
I don’t want to use ReportBuildOptions.AllowMissingMembers
as I want to ensure that if there is a missing field then it should fail.
It seems like there is a mismatch between data provided (objects of regular classes) and the exception message (saying about DataRow
). Please double check that all the pieces of information relate to the same case. Then, if the issue is still there, please provide us a sample template, data, and code reproducing the issue (a standalone console application without compilation errors), so we could check it on our end.