microsoft/ClearScript

Extension method for primitives types

Closed this issue · 4 comments

Is it possible to add extrension methods to primitive type?
For example, I already created a method for String, however the script doesn't reconigze the method.

Hi @sergioamm17,

Is it possible to add extrension methods to primitive type?

If you're using V8, then yes. It just takes a little extra setup. Suppose your extension type looks like this:

public static class Extensions {
    public static void SayHello(this object obj) {
        Console.WriteLine("Hello! I am '{0}'.", obj);
    }
}

You can use it with primitive values on the .NET side:

"foo".SayHello();
123.456.SayHello();

Here's how to add SayHello on the JavaScript side:

dynamic addExtension = engine.Evaluate(@"
    (ctor, extType, name) => 
        ctor.prototype[name] = function () { return extType[name](this.valueOf()); }
");
addExtension(engine.Script.Object, typeof(Extensions).ToHostType(engine), "SayHello");

And now you can do this:

engine.Execute(@"
    'foo'.SayHello();
    123.456.SayHello();
");

Good luck!

Thanks for your response,

Can we create an extension method direct to a Type as String?
We already have this kind of extension over specific types (String Int), but, we are no using the object type directly
For example:

public static class Extensions {
    public static void TestString(this string input, string value) {
        Console.WriteLine($"Hello! I am {value}");
    }
}```


Can we create an extension method direct to a Type as String?

Sure. The first argument to addExtension specifies the target JavaScript constructor. For example (this also shows how to pass additional arguments to the extension method):

dynamic addExtension = engine.Evaluate(@"
    (ctor, extType, name) => 
        ctor.prototype[name] = function () { return extType[name](this.valueOf(), ...arguments); }
");
addExtension(engine.Script.String, typeof(Extensions).ToHostType(engine), "TestString");

And now:

"foo".TestString("bar");
engine.Execute("'foo'.TestString('bar');");

Cheers!

Thank you so much, this is really helpful.

Best regards