DefineProperty() public method

Defines a new object's own property from a property descriptor
Requires an active script context.
public DefineProperty ( EdgeJsPropertyId propertyId, EdgeJsValue propertyDescriptor ) : bool
propertyId EdgeJsPropertyId The ID of the property
propertyDescriptor EdgeJsValue The property descriptor
return bool
        private void ProjectProperties(EdgeJsValue 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;

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

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

                        object thisObj = null;

                        if (instance)
                        {
                            if (!thisValue.HasExternalData)
                            {
                                EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateTypeError(
                                    string.Format(NetCoreStrings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                                EdgeJsErrorHelpers.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)
                            ;

                            EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateError(errorMessage);
                            EdgeJsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

                        EdgeJsValue resultValue = MapToScriptType(result);

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

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

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

                        object thisObj = null;

                        if (instance)
                        {
                            if (!thisValue.HasExternalData)
                            {
                                EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateTypeError(
                                    string.Format(NetCoreStrings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                                EdgeJsErrorHelpers.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)
                            ;

                            EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateError(errorMessage);
                            EdgeJsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

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

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

                target.DefineProperty(propertyName, descriptorValue);
            }
        }