[英]Get System.Type instance from within script (ClearScript)
嘗試從通過 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
object (這包含mscorlib
, System
和System.Core
)
似乎 ClearScript 正在嘗試調用,並且對是否將第一個參數app.MyLibrary.MyEnum
通用參數或將其作為System.Type
參數傳遞感到困惑。
在這種情況下,我該怎么做才能正確調用System.Enum.Parse
function?
答案比我想象的要簡單。 由於 ClearScript 將第一個參數視為通用參數,因此您只需要一個 function 從類型參數返回一個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 還附帶了一個實用程序 class ExtendedHostFunctions
,它提供了幾個有用的實用程序函數,包括一個類似於上述typeOf(T)
的函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.