Example #1
0
        public void Initialize(IntPtr native, Type type, bool isKnownType, int internalIndex)
        {
            // Something is still holding on this obj ref
            Debug.Assert(OnDestroyed == null);

            InternalIndex = internalIndex;

            // If this loops around things could potentially break down if some objects have
            // extremely long lifetime and others have a short lifetime (this id is mostly just
            // used for equality checks)
            unchecked
            {
                Id = sid++;
            }

            Native      = native;
            IsKnownType = isKnownType;

            // TODO: Replace this with lambda compiled constructor as it is is faster
            Managed          = (UObject)Activator.CreateInstance(type);
            Managed.objRef   = this;
            Managed.objRefId = Id;
            Managed.Address  = native;

            // Call the regular Initialize() method unless this object is a managed object currently being initialized
            // (in which case Initialize(objectInitializer) will be called after the managed object has been created)
            if (GCHelper.ManagedObjectBeingInitialized != native)
            {
                Managed.Initialize();
            }

            // A handle to itself for GCHelper C# <-> GCHelper C++
            // Do we need to use Pinned here? Probably.
            ManagedHandle = GCHandle.Alloc(this, GCHandleType.Weak);
        }