Пример #1
0
        private static void RegisterJSObjectProperties(JSObject thisObject, object clrObject)
        {
            var objectType = clrObject.GetType();

            var jsFunctions = objectType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Where(x => x.IsSpecialName == false);            //.Where(x => x.GetCustomAttributes(typeof(JSFunctionAttribute), true).Length > 0);

            foreach (var func in jsFunctions)
            {
                var funcType = func.GetType();
                JSFunctionAttribute propertyDescriber = (JSFunctionAttribute)func.GetCustomAttributes(typeof(JSFunctionAttribute), true).FirstOrDefault();

                var jsPropertyName = func.Name;

                if (propertyDescriber != null && !string.IsNullOrEmpty(propertyDescriber.JSName))
                {
                    jsPropertyName = propertyDescriber.JSName;
                }


                var jsFunc = thisObject.AddFunction(jsPropertyName, JSInvokeMode.DontInvoke);

                jsFunc.Execute += (_, args) =>
                {
                    var funcParams = func.GetParameters();

                    var clrParams = GetParametersFromCfrArguemnts(func.GetParameters(), args.Arguments);


                    var returnValue = func.Invoke(clrObject, clrParams.ToArray());



                    var v8ReturnValue = ClrValueToV8Value(returnValue);

                    args.SetReturnValue(v8ReturnValue);
                };
            }

            var jsProperties = objectType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);            //.Where(x => x.GetCustomAttributes(typeof(JSPropertyAttribute), true).Length > 0);



            foreach (var prop in jsProperties)
            {
                var propertyType = prop.GetType();
                JSPropertyAttribute propertyDescriber = (JSPropertyAttribute)prop.GetCustomAttributes(typeof(JSPropertyAttribute), true).FirstOrDefault();

                var jsPropertyName = prop.Name;

                if (propertyDescriber != null && !string.IsNullOrEmpty(propertyDescriber.JSName))
                {
                    jsPropertyName = propertyDescriber.JSName;
                }

                var thisProperty = thisObject.AddDynamicProperty(jsPropertyName);



                if (prop.CanRead && prop.GetGetMethod() != null && prop.GetGetMethod().IsPublic)
                {
                    thisProperty.PropertyGet += (_, args) =>
                    {
                        args.Retval = ClrValueToV8Value(prop.GetValue(clrObject, null));
                        args.SetReturnValue(true);
                    };
                }
                else
                {
                    thisProperty.PropertyGet += (_, args) =>
                    {
                        args.SetReturnValue(false);
                    };
                }


                if (prop.CanWrite && prop.GetSetMethod() != null && prop.GetSetMethod().IsPublic)
                {
                    thisProperty.PropertySet += (_, args) =>
                    {
                        prop.SetValue(clrObject, V8ValueToClrValue(prop.PropertyType, args.Value), null);
                        args.SetReturnValue(true);
                    };
                }
                else
                {
                    thisProperty.PropertySet += (_, args) =>
                    {
                        args.SetReturnValue(false);
                    };
                }
            }
        }