Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Is there a way to AddHostType with the object named in a string?

$
0
0
Hello,

Yes, ClearScript provides AddHostType overloads that let you specify the type by name. For example:
jse.AddHostType("Random", "System.Random");
However, there are some considerations. First, the above code works only if the type resides in mscorlib or in the currently executing assembly. For other types, you can specify the assembly:
engine.AddHostType("Enumerable", "System.Linq.Enumerable", "System.Core");
However, you might want to avoid doing so. Identifying an assembly based on a short name like "System.Core" requires an expensive brute-force search that incurs a large one-time performance hit (the results of the search are saved in a file for subsequent reuse).

Our recommended approach is to load the type manually via Type.GetType. For non-mscorlib types this does require an assembly-qualified name:
engine.AddHostType("Enumerable", Type.GetType("System.Linq.Enumerable, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"));
Good luck!

Viewing all articles
Browse latest Browse all 2297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>