Closed vbornand closed 1 year ago
Hi @vbornand,
How can I have receive the same object returned by the method
getDeviceByCode2
ClearScript provides an extension method – ToRestrictedHostObject
– that may be of assistance. For example:
[ScriptUsage]
public object GetDeviceByCode3(string code)
{
return this.GetDeviceByCode(code).ToRestrictedHostObject<ITelegramBot>();
}
Please note that ToRestrictedHostObject
returns a host object – an instance of an internal ClearScript class intended to be passed or returned to script code. Such an object isn't useful to the host application.
To complexify, the type
ITelegramBot
provides from a plugin, and I don't want the plugin have a reference on ClearScript, so I can't use theScriptAccess
attributes.
There's a way to "virtually" add ClearScript attributes to external resources. You can read about it here.
Good luck!
Thanks @ClearScriptLib for your quick answer.
I successfully attended my goal using the ToRestrictedHostObject
function. (Using reflection before to call it, because ITelegramBot
is defined in a plugin and not known at the compile time by the app hosting the script engine.)
It's not clean code, but if it can help somebody:
[ScriptUsage]
public object GetDeviceByCode3(string code)
{
var tBot = this.GetDeviceByCode(code);
var telegramBotType = this.sharedTypes.First(t => t.Name.Contains("ITelegramBot"));
//Dirty way to get the method
var ext = typeof(Microsoft.ClearScript.Extensions)
.GetMethods(BindingFlags.Static | BindingFlags.Public)
.FirstOrDefault(m => m.Name == "ToRestrictedHostObject" && m.GetParameters().Length == 2);
var r = ext.MakeGenericMethod(telegramBotType).Invoke(null, new object[] { tBot, jsEngine });
return r;
}
If the method ToRestrictedHostObject
provided an overload with the type as parameter (and not only available with genericity), it would be great.
Thanks for the link about to add attributes on external resources. Maybe it is the best solution, and I can use pretty complex logic to know on which methods are script visible depending which interface the method implements. I think it would be a cleaner solution as the one with the ToRestrictedHostObject
.
Thanks again
Valentin
Hi Valentin,
If the method
ToRestrictedHostObject
provided an overload with the type as parameter (and not only available with genericity), it would be great.
Good idea! We'll add that in the next release.
Thank you!
Version 7.3.5 added a pair of ToRestrictedHostObject
overloads.
Hi,
I have a very specific use case, I want to return an object with a specific type only known on the runtime, and I don't want the javascript caller need to specify it.
I have currently the 2 methods:
The first method works well, but I need to specify the type (T) in the javascript:
Only the methods defined on the
ITelegramBot
interface can be invoked from the javascript. It is the expected behavior.With the second method, I don't need to specify the type in the javascript:
But here all the methods of the runtime object are visible and can be called. And I would like to limit the methods callable.
How can I have receive the same object returned by the method the method
getDeviceByCode2
in the methodgetDeviceByCode3
?To complexify, the type
ITelegramBot
provides from a plugin, and I don't want the plugin have a reference on ClearScript, so I can't use theScriptAccess
attributes.Thanks for your help.