Issues working with known types

Hello,
I’m having some issues working with known types.
I have created a known type which is really basic

public class Custom 
{
    public static string ShowMe { get {return "ShowThat"; } }
    public static string DoSomething() { return "DoSomething"; }
}

and added it to the engine like documented
engine.KnownTypes.Add(typeof(Custom));

Now we face some limitations because for this to work, the property and method needs to be static, which is probably for a reason. Our need would be to get data from an outside source by calling a service that needs to be injected into the known type class. Having the limitation of static property/method is not possible with static typing. Is there a reason why this need to be static? Is there a way to do what we need to achieve? Example below of what we need to perform:

public class Custom
{
	private readonly IKernelProvider kernelProvider;
	public Custom(IKernelProvider kernelProvider)
	{
		this.kernelProvider = kernelProvider;
	}

    public string ShowMe { get {return kernelProvider.GetSomeValue(); } }
    public string DoSomething() { return kernelProvider.GetSomeValue(); }
}

@missraphie,

We have logged this problem in our issue tracking system. Your ticket number is WORDSNET-22518. We will further look into the details of this problem and will keep you updated here on the status of the linked ticket. We apologize for any inconvenience.

1 Like

@missraphie,

As per Setting up Known External Types and ReportingEngine.KnownTypes Property, ReportingEngine.KnownTypes is used only when a type itself is accessed in a template expression. For example, when invoking static members of a type or performing a type cast.

When an instance (that is, non-static) member of an object is accessed in a template expression, there is no need to add the object’s type to ReportingEngine.KnownTypes. All you need to make your scenario work is to pass an object as a data source and then you can access the object’s instance members in template expressions just right away.

@awais.hafeez
I found a workaround for my case and it’s working.

The issue here is that in the template, we let the editor enter whatever he wants to be evaluated in the Custom class and we have no idea of the object type. This looks like this:
<<foreach [client in Custom.GetList("organization.clients")]>>
and the param inside GetList is evaluated and returns a list of objects received from a call to another service endpoint. Backend never knows the object types so that’s why we used the KnownTypes feature.

To overcome the static issue, we only had to add a helper method in the class

private static IKernelProvider _kernelProvider;
public static void Configure(IKernelProvider kernelProvider)
{
	_kernelProvider = kernelProvider;
} 

and in startup.cs, make a call to this static method in Configure method
Custom.Configure(app.ApplicationServices.GetService<IKernelProvider>());
provisioning the static class the correct DI implementation.

Thanks for your help!

@missraphie,

We have logged these details in our issue tracking system and will keep you posted here on any further updates.

@missraphie,

Regarding WORDSNET-22518, since we had already provided an explanation on how to use ReportingEngine.KnownTypes and you were also able to resolve the problem on your end; we have now concluded to close this issue with “not a bug” status. In case you have further inquiries or may need any help in future, please let us know by posting a new thread in Aspose.Words’ forum.