Created
May 2, 2019 01:14
-
-
Save rjamesnw/6a0b107c22a4b702d8de45944959be62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ScriptObject(ScriptMemberSecurity.NoAcccess)] // (default all members to "no access") | |
public class MyJSObject | |
{ | |
[Inject] // (inject a value; will only work if the type binder creates the instance, or associates a binder to the instance) | |
//#pragma warning disable 0649 // (assigned by the type binder - OR simply explicitly set a null value to remove the warning) | |
TypeBinder _TypeBinder = null; | |
//#pragma warning restore 0649 | |
public MyJSObject() | |
{ | |
} | |
Handle _Value = Handle.Empty; | |
[ScriptMember(ScriptMemberSecurity.ReadWrite)] | |
public InternalHandle this[int index] | |
{ | |
get | |
{ | |
return _Value; | |
} | |
set | |
{ | |
_Value = _TypeBinder.Engine.CreateValue(value + 1); | |
} | |
} | |
public class CustomBinder : ObjectBinder<MyJSObject> | |
{ | |
// You DO NOT HAVE TO override both of these; here for example only. | |
public override bool GetInjectedValueforType(Type type, out object value) | |
{ | |
return base.GetInjectedValueforType(type, out value); | |
} | |
public override void OnInjection(MemberInfo memberInfo) | |
{ | |
base.OnInjection(memberInfo); | |
} | |
} | |
} | |
/* *** Used like this: *** | |
// ... register the type and get the type binder ... | |
var typeBinder = Engine.RegisterType<MyJSObject>(); // (or you could also call 'Engine.GetTypeBinder()' after calling 'SetProperty(type)') | |
// ... we can provide a custom binder by hooking into an event ... | |
typeBinder.OnGetObjectBinder = (tb, obj, initializeBinder) | |
=> tb.CreateObjectBinder<MyJSObject.CustomBinder, object>(obj, initializeBinder); | |
// ... you can also associate your own ObjectBinder with your instance ... | |
// var obj = typeBinder.CreateObjectBinder<MyJSObject.CustomBinder, MyJSObject>(new MyJSObject()); // (just example only) | |
// Engine.GlobalObject.SetProperty("myJSObject", obj); | |
Engine.GlobalObject.SetProperty(typeof(MyJSObject)); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment