尝试从通过ClearScript托管的脚本内调用Enum.Parse时出现异常
错误
Error: The non-generic method 'System.Enum.Parse(System.Type, string)' cannot be used with type arguments--- Script error details follow ---
Error: The non-generic method 'System.Enum.Parse(System.Type, string)' cannot be used with type arguments
at translateParameterValue (Script [temp]:11:27) -> return clr.System.Enum.Parse(app.MyLibrary.MyEnum, value);脚本
return clr.System.Enum.Parse(app.MyLibrary.MyEnum, value);我非常确定我正确地注册了clr对象(它包含mscorlib、System和System.Core)
似乎ClearScript正在尝试调用,并且正在困惑是将第一个参数app.MyLibrary.MyEnum设为泛型参数,还是将其作为System.Type参数传递。
问题
在这种情况下,如何才能正确调用System.Enum.Parse函数?
发布于 2020-04-11 16:46:12
答案比我想象的要简单。由于ClearScript将第一个参数视为泛型参数,因此您只需要一个从类型参数返回System.Type实例的函数,该函数可以简单地如下所示:
class Utility
{
public Type GetType<T>() {
return typeof(T);
}
}然后将其注册到您的ScriptEngine
_engine.AddHostObject("Utility", new Utility());然后在脚本中使用它,如下所示:
return clr.System.Enum.Parse(Utility.GetType(nepes.DecaTech.CoreData.ProcessStates), value);ClearScript还附带了一个实用程序类ExtendedHostFunctions,它提供了几个有用的实用程序函数,包括一个类似于上面的typeOf(T)的实用程序函数。
https://stackoverflow.com/questions/61153434
复制相似问题