Ejemplo n.º 1
0
        /// <summary>
        /// Augments the hosting type constructor with the logic required to initialize the backing pointer field.
        /// </summary>
        /// <param name="entrypointName">The name of the entry point.</param>
        /// <param name="backingFieldType">The type of the backing field.</param>
        /// <param name="backingField">The backing pointer field.</param>
        private void AugmentHostingTypeConstructorWithNativeInitialization
        (
            [NotNull] string entrypointName,
            [NotNull] Type backingFieldType,
            [NotNull] FieldInfo backingField
        )
        {
            TargetTypeConstructorIL.EmitLoadArgument(0);
            TargetTypeConstructorIL.EmitLoadArgument(0);

            if (Options.HasFlagFast(UseLazyBinding))
            {
                var lambdaBuilder = GenerateSymbolLoadingLambda(entrypointName);
                GenerateLazyLoadedObject(lambdaBuilder, backingFieldType);
            }
            else
            {
                var loadPointerMethod = typeof(NativeLibraryBase).GetMethod
                                        (
                    nameof(NativeLibraryBase.LoadSymbol),
                    BindingFlags.NonPublic | BindingFlags.Instance
                                        );

                TargetTypeConstructorIL.EmitConstantString(entrypointName);
                TargetTypeConstructorIL.EmitCallDirect(loadPointerMethod);
            }

            TargetTypeConstructorIL.EmitSetField(backingField);
        }
Ejemplo n.º 2
0
        private void AugmentHostingTypeConstructor
        (
            [NotNull] string symbolName,
            [NotNull] FieldInfo propertyFieldBuilder
        )
        {
            var loadSymbolMethod = typeof(NativeLibraryBase).GetMethod
                                   (
                nameof(NativeLibraryBase.LoadSymbol),
                BindingFlags.NonPublic | BindingFlags.Instance
                                   );

            TargetTypeConstructorIL.EmitLoadArgument(0);
            TargetTypeConstructorIL.EmitLoadArgument(0);

            if (Options.HasFlagFast(UseLazyBinding))
            {
                var lambdaBuilder = GenerateSymbolLoadingLambda(symbolName);
                GenerateLazyLoadedObject(lambdaBuilder, typeof(IntPtr));
            }
            else
            {
                TargetTypeConstructorIL.EmitConstantString(symbolName);
                TargetTypeConstructorIL.EmitCallDirect(loadSymbolMethod);
            }

            TargetTypeConstructorIL.EmitSetField(propertyFieldBuilder);
        }