Skip to content

How to have a return type known only at runtime? #437

@vbornand

Description

@vbornand

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:

[ScriptUsage]
public T GetDeviceByCode2<T>(string code) where T : class
{
    return this.GetDeviceByCode(code) as T;
}

[ScriptUsage]
public object GetDeviceByCode3(string code)
{ 
    return this.GetDeviceByCode(code) as ITelegramBot; //ITelegramBot is here hardcoded, but in real world, the type is not always the same and depends of the parameter value.
}

The first method works well, but I need to specify the type (T) in the javascript:

var bot = house.getDeviceByCode2(ITelegramBot, "MyBot");

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:

var bot = house.getDeviceByCode3("MyBot");

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 method getDeviceByCode3 ?

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 the ScriptAccess attributes.

Thanks for your help.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions