예제 #1
0
        // Bootstrapped from https://github.com/canton7/RestEase/blob/master/src/RestEase/Implementation/ImplementationBuilder.cs

        private Type BuildImplementationType(AccessorDescription description)
        {
            var accessorType = description.AccessorType;

            var implementationTypeName = CreateImplementationTypeName(accessorType);
            var typeBuilder            = _moduleBuilder.DefineType(implementationTypeName, TypeAttributes.Public | TypeAttributes.Sealed);

            typeBuilder.AddInterfaceImplementation(accessorType);

            var dispatchFieldBuilder = typeBuilder.DefineField("_dispatch", typeof(IDispatcher), FieldAttributes.Private | FieldAttributes.InitOnly);
            var instanceFieldBuilder = typeBuilder.DefineField("_instance", typeof(object), FieldAttributes.Private | FieldAttributes.InitOnly);

            AddInstanceCtor(typeBuilder, dispatchFieldBuilder, instanceFieldBuilder);

            AddFieldAccessor(description.DispatcherProperty, typeBuilder, dispatchFieldBuilder);
            AddFieldAccessor(description.InstanceProperty, typeBuilder, instanceFieldBuilder);

            foreach (var member in description.Members)
            {
                var id = member.Key;
                var memberDescription = member.Value;
                switch (memberDescription)
                {
                case PropertyDescription propertyDescription:
                    AddPropertyDispatch(id, propertyDescription, typeBuilder, instanceFieldBuilder, dispatchFieldBuilder);
                    break;

                case FieldDescription fieldDescription:
                    AddFieldDispatch(id, fieldDescription, typeBuilder, instanceFieldBuilder, dispatchFieldBuilder);
                    break;

                case MethodDescription methodDescription:
                    AddMethodDispatch(id, methodDescription, typeBuilder, instanceFieldBuilder, dispatchFieldBuilder);
                    break;
                }
            }

            try
            {
                var constructedType = typeBuilder.CreateTypeInfo().AsType();
                return(constructedType);
            }
            catch (TypeLoadException e)
            {
                // TODO
                throw;
            }
        }
예제 #2
0
 public Dispatcher(AccessorDescription description)
 {
     Description = description;
 }
예제 #3
0
        public TAccessor Implement <TAccessor>(object instance, IDispatcher dispatcher, AccessorDescription description) where TAccessor : ITypedReflectAccessor
        {
            var type = BuildImplementationType(description);

            var implementation = Activator.CreateInstance(type, dispatcher, instance);

            return((TAccessor)implementation);
        }