CreateObjectFromTemplate() private method

private CreateObjectFromTemplate ( NativeObjectTemplateProxy objectTemplateProxy, Int32 objID ) : HandleProxy*
objectTemplateProxy NativeObjectTemplateProxy
objID System.Int32
return HandleProxy*
示例#1
0
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Creates an object of the specified type and returns it.  A V8 object is also created and associated with it.
        /// <para>Performance note: Creating 'V8NativeObject' type objects are allowed, but an object template is not needed for those.  If you create a
        /// 'V8NativeObject' object from a template, it simply wraps the native object create by the template, and property interceptors (call-backs) are still
        /// triggered.  While native objects are faster than managed ones, creating 'V8NativeObject' objects using 'V8Engine.CreateObject()' does not use
        /// interceptors and is many times faster than template objects.  If it is desired to create 'V8NativeObject' objects from templates, consider calling
        /// '<seealso cref="UnregisterPropertyInterceptors()"/>' on the object template to make them the same speed as if 'V8Engine.CreateObject()' was used.</para>
        /// </summary>
        /// <typeparam name="T">The type of managed object to create, which must implement 'IV8NativeObject',</typeparam>
        /// <param name="initialize">If true (default) then then 'IV8NativeObject.Initialize()' is called on the created object before returning.</param>
        public T CreateObject <T>(bool initialize = true) where T : V8NativeObject, new()
        {
            if (_Engine == null)
            {
                throw new InvalidOperationException("You must create object templates by calling one of the 'V8Engine.CreateObjectTemplate()' overloads.");
            }

            if (_NativeObjectTemplateProxy == null)
            {
                throw new InvalidOperationException("This managed object template is either not initialized, or does not support creating V8 objects.");
            }

            // ... create object locally first and index it ...

            var obj = _Engine._CreateManagedObject <T>(this, InternalHandle.Empty);

            // ... create the native object and associated it to the managed wrapper ...

            try
            {
                obj.Handle._Set(V8NetProxy.CreateObjectFromTemplate(_NativeObjectTemplateProxy, obj.ID));
                // (note: setting '_NativeObject' also updates it's '_ManagedObject' field if necessary.
            }
            catch (Exception ex)
            {
                // ... something went wrong, so remove the new managed object ...
                _Engine._RemoveObjectWeakReference(obj.ID);
                throw ex;
            }

            if (initialize)
            {
                obj.Initialize(false, null);
            }

            return((T)obj);
        }