示例#1
0
文件: Bind.cs 项目: SirJosh3917/Biind
        public Bind(BindSpecifications <TType, TInterface> bindSpecifications, BindAssembly bindAssembly)
        {
            _specs        = bindSpecifications;
            _bindAssembly = bindAssembly;

            _generatedType = RuntimeTypeCreationLogic.Build
                             (
                _bindAssembly.DefineType
                (
                    interfaces: new[] { typeof(TInterface) }
                ),
                _specs
                             );

            _newDelegate = RuntimeTypeCreationLogic.CreateFactory <TType, TInterface>
                           (
                returnType: typeof(TInterface),
                parameterTypes: new[] { typeof(TType) },
                constructor: _generatedType.GetConstructor(new[] { typeof(TType) })
                           );
        }
示例#2
0
        public static Type Build <TType, TInterface>
        (
            TypeBuilder typeBuilder,
            BindSpecifications <TType, TInterface> bindSpecifications
        )
        {
            var instance = typeBuilder.DefineField("_instance", typeof(TType), FieldAttributes.Private);

            var ctor = CreateConstructor <TType>(typeBuilder, instance);

            foreach (var(targetMethod, interfaceMethod) in bindSpecifications.FunctionMappings)
            {
                BindMethod
                (
                    typeBuilder: typeBuilder,
                    instance: instance,
                    targetMethod: targetMethod,
                    interfaceMethod: interfaceMethod
                );
            }

            foreach (var(targetProperty, interfaceProperty) in bindSpecifications.PropertyMappings)
            {
                BindProperty
                (
                    typeBuilder: typeBuilder,
                    instance: instance,
                    targetProperty: targetProperty,
                    interfaceProperty: interfaceProperty
                );
            }

            return
                (#if NET472
                 typeBuilder.CreateType());
#else
                 typeBuilder.CreateTypeInfo().AsType();
#endif
        }