Example #1
0
        /// <summary>
        /// Binds all the fields for the instance.
        /// </summary>
        private void BindFields(JsBinding binding, IHostType hostType, object instance)
        {
            // TODO: Seems like we can hoist this into JsInterop, which would allow us refactor
            // TODO: out the builder class completely.
            var fields = hostType.FieldNames;

            for (int i = 0; i < fields.Count; ++i)
            {
                var fieldName = fields[i];

                binding.AddProperty(
                    fieldName,
                    (v, s, args, argLength, data) =>
                {
                    var fieldInfo = hostType.FieldFor(fieldName).Field;
                    var result    = fieldInfo.GetValue(instance);

                    var returnType = JsConversions.TypeFor(result, fieldInfo.FieldType);
                    return(_interop.ToJsObject(result, returnType));
                },
                    (v, s, args, argLength, data) =>
                {
                    var fieldInfo = hostType.FieldFor(fieldName).Field;
                    var fieldType = fieldInfo.FieldType;

                    var value = _interop.ToHostObject(args[1], fieldType);
                    fieldInfo.SetValue(instance, value);

                    return(JavaScriptValue.Invalid);
                });
            }
        }
Example #2
0
        /// <summary>
        /// Binds all public properties for the instance.
        /// </summary>
        private void BindProperties(JsBinding binding, IHostType hostType, object instance)
        {
            // TODO: Seems like we can hoist this into JsInterop, which would allow us refactor
            // TODO: out the builder class completely.
            var properties = hostType.PropertyNames;

            for (int i = 0; i < properties.Count; ++i)
            {
                var propertyName = properties[i];

                binding.AddProperty(
                    propertyName,
                    (v, s, args, argLength, data) =>
                {
                    var get    = hostType.PropertyFor(propertyName).Getter;
                    var result = get.Invoke(instance, EmptyParameters);

                    var returnType = JsConversions.TypeFor(result, get.ReturnType);
                    return(_interop.ToJsObject(result, returnType));
                },
                    (v, s, args, argLength, data) =>
                {
                    var hostProperty = hostType.PropertyFor(propertyName);
                    var propType     = hostProperty.PropertyType;
                    var set          = hostProperty.Setter;

                    var value = _interop.ToHostObject(args[1], propType);
                    set.Invoke(instance, new[] { value });
                    return(JavaScriptValue.Invalid);
                });
            }
        }