CreateObject() public static method

Creates a new Object
Requires an active script context.
public static CreateObject ( ) : IeJsValue
return IeJsValue
コード例 #1
0
        private void ProjectProperties(IeJsValue target, Type type, bool instance)
        {
            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);

            PropertyInfo[] properties = type.GetProperties(defaultBindingFlags);

            foreach (PropertyInfo property in properties)
            {
                string propertyName = property.Name;

                IeJsValue descriptorValue = IeJsValue.CreateObject();
                descriptorValue.SetProperty("enumerable", IeJsValue.True, true);

                if (property.GetGetMethod() != null)
                {
                    IeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                    {
                        IeJsValue thisValue      = args[0];
                        IeJsValue undefinedValue = IeJsValue.Undefined;

                        object thisObj = null;

                        if (instance)
                        {
                            if (!thisValue.HasExternalData)
                            {
                                IeJsValue errorValue = IeJsErrorHelpers.CreateTypeError(
                                    string.Format(NetCoreStrings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                                IeJsErrorHelpers.SetException(errorValue);

                                return(undefinedValue);
                            }

                            thisObj = MapToHostType(thisValue);
                        }

                        object result;

                        try
                        {
                            result = property.GetValue(thisObj, new object[0]);
                        }
                        catch (Exception e)
                        {
                            string errorMessage = instance ?
                                                  string.Format(
                                NetCoreStrings.Runtime_HostObjectPropertyGettingFailed, propertyName, e.Message)
                                                                :
                                                  string.Format(
                                NetCoreStrings.Runtime_HostTypePropertyGettingFailed, propertyName, typeName, e.Message)
                            ;

                            IeJsValue errorValue = IeJsErrorHelpers.CreateError(errorMessage);
                            IeJsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

                        IeJsValue resultValue = MapToScriptType(result);

                        return(resultValue);
                    };
                    _nativeFunctions.Add(nativeFunction);

                    IeJsValue getMethodValue = IeJsValue.CreateFunction(nativeFunction);
                    descriptorValue.SetProperty("get", getMethodValue, true);
                }

                if (property.GetSetMethod() != null)
                {
                    IeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                    {
                        IeJsValue thisValue      = args[0];
                        IeJsValue undefinedValue = IeJsValue.Undefined;

                        object thisObj = null;

                        if (instance)
                        {
                            if (!thisValue.HasExternalData)
                            {
                                IeJsValue errorValue = IeJsErrorHelpers.CreateTypeError(
                                    string.Format(NetCoreStrings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                                IeJsErrorHelpers.SetException(errorValue);

                                return(undefinedValue);
                            }

                            thisObj = MapToHostType(thisValue);
                        }

                        object value = MapToHostType(args.Skip(1).First());
                        ReflectionHelpers.FixPropertyValueType(ref value, property);

                        try
                        {
                            property.SetValue(thisObj, value, new object[0]);
                        }
                        catch (Exception e)
                        {
                            string errorMessage = instance ?
                                                  string.Format(
                                NetCoreStrings.Runtime_HostObjectPropertySettingFailed, propertyName, e.Message)
                                                                :
                                                  string.Format(
                                NetCoreStrings.Runtime_HostTypePropertySettingFailed, propertyName, typeName, e.Message)
                            ;

                            IeJsValue errorValue = IeJsErrorHelpers.CreateError(errorMessage);
                            IeJsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

                        return(undefinedValue);
                    };
                    _nativeFunctions.Add(nativeFunction);

                    IeJsValue setMethodValue = IeJsValue.CreateFunction(nativeFunction);
                    descriptorValue.SetProperty("set", setMethodValue, true);
                }

                target.DefineProperty(propertyName, descriptorValue);
            }
        }