コード例 #1
0
        public virtual void DefineType(ProxyGeneratorContext context)
        {
            var serviceType = context.ServiceType;

            var(parent, interfaceTypes) = serviceType.IsInterface
                    ? (typeof(object), new Type[] { serviceType })
                    : (serviceType, Type.EmptyTypes);
            context.TypeBuilder = context.ModuleBuilder.DefineType(context.ProxyTypeName, TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed, parent, interfaceTypes);
        }
コード例 #2
0
        public virtual void DefineConstructors(ProxyGeneratorContext context)
        {
            var constructorInfos = context.ServiceType.GetTypeInfo().DeclaredConstructors.Where(c => !c.IsStatic && (c.IsPublic || c.IsFamily || c.IsFamilyAndAssembly || c.IsFamilyOrAssembly)).ToArray();

            if (constructorInfos.Length == 0)
            {
                var constructorBuilder = context.TypeBuilder.DefineConstructor(MethodAttributes.Public, ObjectCtor.CallingConvention, new Type[] { typeof(IInterceptorFactory) });

                constructorBuilder.DefineParameter(1, ParameterAttributes.None, InterceptorFactory);

                var ilGen = constructorBuilder.GetILGenerator();
                ilGen.EmitThis();
                ilGen.Emit(OpCodes.Call, ObjectCtor);

                ilGen.EmitThis();
                ilGen.EmitLoadArg(1);
                ilGen.Emit(OpCodes.Stfld, context.Fields[InterceptorFactory]);

                ilGen.Emit(OpCodes.Ret);
            }
            else
            {
                foreach (var constructor in constructorInfos)
                {
                    Type[] parameterTypes = new Type[] { typeof(IInterceptorFactory) }.Concat(constructor.GetParameters().Select(i => i.ParameterType)).ToArray();
                    var    constructorBuilder = context.TypeBuilder.DefineConstructor(context.ServiceType.IsAbstract ? constructor.Attributes | MethodAttributes.Public : constructor.Attributes, constructor.CallingConvention, parameterTypes);
                    foreach (var customAttributeData in constructor.CustomAttributes)
                    {
                        constructorBuilder.SetCustomAttribute(DefineCustomAttribute(customAttributeData));
                    }
                    DefineParameters(constructor, constructorBuilder);

                    var ilGen = constructorBuilder.GetILGenerator();

                    ilGen.EmitThis();
                    ilGen.EmitLoadArg(1);
                    ilGen.Emit(OpCodes.Stfld, context.Fields[InterceptorFactory]);

                    ilGen.EmitThis();
                    for (var i = 2; i <= parameterTypes.Length; i++)
                    {
                        ilGen.EmitLoadArg(i);
                    }
                    ilGen.Emit(OpCodes.Call, constructor);

                    ilGen.Emit(OpCodes.Ret);
                }
            }
        }
コード例 #3
0
 public virtual Type CreateProxyType(ProxyGeneratorContext context)
 {
     DefineType(context);
     DefineFields(context);
     DefineConstructors(context);
     //var dp = implTypeBuilder.DefineProperty(p.Name, p.Attributes, p.PropertyType, Type.EmptyTypes);
     //if (p.CanRead)
     //{
     //    var method = MethodBuilderUtils.DefineClassMethod(p.GetMethod, implType, typeDesc);
     //    dp.SetGetMethod(method);
     //}
     //if (p.CanWrite)
     //{
     //    var method = MethodBuilderUtils.DefineClassMethod(p.SetMethod, implType, typeDesc);
     //    dp.SetSetMethod(method);
     //}
     return(context.TypeBuilder.CreateTypeInfo().AsType());
 }
コード例 #4
0
 public virtual void DefineFields(ProxyGeneratorContext context)
 {
     context.Fields.Add(Instance, context.TypeBuilder.DefineField(Instance, context.ServiceType, FieldAttributes.Private));
     context.Fields.Add(InterceptorFactory, context.TypeBuilder.DefineField(InterceptorFactory, typeof(IInterceptorFactory), FieldAttributes.Private));
 }