-
I'm currently exposing C# class instances to JavaScript using: _jsEngine.SetValue(moduleName, instance); This correctly exposes all methods, but maintains C# naming conventions (PascalCase), so JavaScript code must call methods like: logger.LogInfo("My message"); Desired Behavior
What I've Tried // First, register the original instance with its PascalCase methods
_jsEngine.SetValue(scriptModuleAttribute.Name, instance);
// Then create a wrapper with snake_case method names
var jsBuilder = new StringBuilder();
jsBuilder.AppendLine($"const original_{scriptModuleAttribute.Name} = {scriptModuleAttribute.Name};");
jsBuilder.AppendLine($"{scriptModuleAttribute.Name} = {{");
var methods = module.ModuleType.GetMethods(...)
.Where(m => m.GetCustomAttribute<ScriptFunctionAttribute>() != null);
foreach (var method in methods)
{
string snakeCaseName = ToSnakeCase(method.Name);
jsBuilder.Append($" {snakeCaseName}: original_{scriptModuleAttribute.Name}.{method.Name}");
}
jsBuilder.AppendLine("};");
_jsEngine.Execute(jsBuilder.ToString()); I've also tried to create delegates directly: var engineValue = _jsEngine.Evaluate("({})");
foreach (var method in methods)
{
string snakeCaseName = ToSnakeCase(method.Name);
// This fails with: System.ArgumentException: Type must derive from Delegate.
_jsEngine.SetValue(method.Name, method.CreateDelegate(typeof(Delegate), instance));
// ...
} Questions
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can configure the TypeResolver: Line 339 in 6b7414d MemberNameCreator handles naming. |
Beta Was this translation helpful? Give feedback.
-
oh, thank you! :) |
Beta Was this translation helpful? Give feedback.
You can configure the TypeResolver:
jint/Jint/Options.cs
Line 339 in 6b7414d
MemberNameCreator handles naming.