private MethodGenerator ExplicitlyImplementedInterfaceMethodGenerator( MetaMethod method, ClassEmitter @class, OverrideMethodDelegate overrideMethod ) { var @delegate = GetDelegateType(method, @class); var contributor = GetContributor(@delegate, method); var invocation = new InheritanceInvocationTypeGenerator( targetType, method, null, contributor ) .Generate(@class, namingScope) .BuildType(); return(new MethodWithInvocationGenerator( method, @class.GetField("__interceptors"), invocation, (c, m) => new TypeTokenExpression(targetType), overrideMethod, contributor )); }
protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class, OverrideMethodDelegate overrideMethod) { if (methodsToSkip.Contains(method.Method)) { return(null); } if (!method.Proxyable) { return(new MinimialisticMethodGenerator(method, overrideMethod)); } if (ExplicitlyImplementedInterfaceMethod(method)) { return(ExplicitlyImplementedInterfaceMethodGenerator(method, @class, overrideMethod)); } var invocation = GetInvocationType(method, @class); GetTargetExpressionDelegate getTargetTypeExpression = (c, m) => new TypeTokenExpression(targetType); return(new MethodWithInvocationGenerator(method, @class.GetField("__interceptors"), invocation, getTargetTypeExpression, getTargetTypeExpression, overrideMethod, null)); }
protected override void CustomizeGetObjectData( CodeBuilder codebuilder, ArgumentReference serializationInfo, ArgumentReference streamingContext, ClassEmitter emitter ) { var targetField = emitter.GetField("__target"); codebuilder.AddStatement( new MethodInvocationExpression( serializationInfo, SerializationInfoMethods.AddValue_Object, new LiteralStringExpression("__targetFieldType"), new LiteralStringExpression( targetField.Reference.FieldType.AssemblyQualifiedName ) ) ); codebuilder.AddStatement( new MethodInvocationExpression( serializationInfo, SerializationInfoMethods.AddValue_Object, new LiteralStringExpression("__theInterface"), new LiteralStringExpression(targetType.AssemblyQualifiedName) ) ); }
protected override MethodGenerator GetMethodGenerator( MetaMethod method, ClassEmitter @class, OverrideMethodDelegate overrideMethod ) { if (!method.Proxyable) { return(new ForwardingMethodGenerator( method, overrideMethod, (c, i) => fields[i.DeclaringType] )); } var invocation = GetInvocationType(method, @class); return(new MethodWithInvocationGenerator( method, @class.GetField("__interceptors"), invocation, getTargetExpression, overrideMethod, null )); }
protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod) { if (methodsToSkip.Contains(method.Method)) { return(null); } if (!method.Proxyable) { return(new MinimialisticMethodGenerator(method, overrideMethod)); } if (IsDirectlyAccessible(method) == false) { return(IndirectlyCalledMethodGenerator(method, @class, options, overrideMethod)); } var invocation = GetInvocationType(method, @class, options); return(new MethodWithInvocationGenerator(method, @class.GetField("__interceptors"), invocation, (c, m) => c.GetField("__target").ToExpression(), overrideMethod, null)); }
private Expression SetMethodInterceptors(ClassEmitter @class, INamingScope namingScope, MethodEmitter emitter, Expression proxiedMethodTokenExpression) { var selector = @class.GetField("__selector"); if (selector == null) { return(null); } var methodInterceptorsField = BuildMethodInterceptorsField(@class, MethodToOverride, namingScope); var emptyInterceptors = new NewArrayExpression(0, typeof(IInterceptor)); var selectInterceptors = new MethodInvocationExpression(selector, InterceptorSelectorMethods.SelectInterceptors, new MethodInvocationExpression(null, TypeUtilMethods.GetTypeOrNull, getTargetExpression(@class, MethodToOverride)), proxiedMethodTokenExpression, interceptors.ToExpression()) { VirtualCall = true }; emitter.CodeBuilder.AddExpression( new IfNullExpression(methodInterceptorsField, new AssignStatement(methodInterceptorsField, new NullCoalescingOperatorExpression(selectInterceptors, emptyInterceptors)))); return(methodInterceptorsField.ToExpression()); }
private MethodGenerator IndirectlyCalledMethodGenerator( MetaMethod method, ClassEmitter proxy, OverrideMethodDelegate overrideMethod, bool skipInterceptors = false ) { var @delegate = GetDelegateType(method, proxy); var contributor = GetContributor(@delegate, method); var invocation = new CompositionInvocationTypeGenerator( targetType, method, null, false, contributor ) .Generate(proxy, namingScope) .BuildType(); return(new MethodWithInvocationGenerator( method, skipInterceptors ? NullExpression.Instance : proxy.GetField("__interceptors"), invocation, (c, m) => c.GetField("__target"), overrideMethod, contributor )); }
private Expression[] GetCtorArguments(ClassEmitter @class, INamingScope namingScope, Expression proxiedMethodTokenExpression, TypeReference[] dereferencedArguments) { var selector = @class.GetField("__selector"); if (selector != null) { return(new[] { getTargetExpression(@class, MethodToOverride), SelfReference.Self.ToExpression(), interceptors.ToExpression(), proxiedMethodTokenExpression, new ReferencesToObjectArrayExpression(dereferencedArguments), selector.ToExpression(), new AddressOfReferenceExpression(BuildMethodInterceptorsField(@class, MethodToOverride, namingScope)) }); } return(new[] { getTargetExpression(@class, MethodToOverride), SelfReference.Self.ToExpression(), interceptors.ToExpression(), proxiedMethodTokenExpression, new ReferencesToObjectArrayExpression(dereferencedArguments) }); }
public void Generate(ClassEmitter emitter) { var interceptorsField = emitter.GetField("__interceptors"); var targetReference = getTargetReference(); var dynProxyGetTarget = emitter.CreateMethod(nameof(IProxyTargetAccessor.DynProxyGetTarget), typeof(object)); dynProxyGetTarget.CodeBuilder.AddStatement( new ReturnStatement(new ConvertExpression(typeof(object), targetType, targetReference))); var dynProxySetTarget = emitter.CreateMethod(nameof(IProxyTargetAccessor.DynProxySetTarget), typeof(void), typeof(object)); // we can only change the target of the interface proxy if (targetReference is FieldReference targetField) { dynProxySetTarget.CodeBuilder.AddStatement( new AssignStatement(targetField, new ConvertExpression(targetField.FieldBuilder.FieldType, dynProxySetTarget.Arguments[0]))); } else { dynProxySetTarget.CodeBuilder.AddStatement( new ThrowStatement(typeof(InvalidOperationException), "Cannot change the target of the class proxy.")); } dynProxySetTarget.CodeBuilder.AddStatement(new ReturnStatement()); var getInterceptors = emitter.CreateMethod(nameof(IProxyTargetAccessor.GetInterceptors), typeof(IInterceptor[])); getInterceptors.CodeBuilder.AddStatement( new ReturnStatement(interceptorsField)); }
public override void Generate(ClassEmitter @class, ProxyGenerationOptions options) { var interceptors = @class.GetField("__interceptors"); ImplementProxyTargetAccessor(@class, interceptors); foreach (var attribute in targetType.GetNonInheritableAttributes()) { @class.DefineCustomAttribute(attribute); } }
public virtual void Generate(ClassEmitter @class, ProxyGenerationOptions options) { var interceptors = @class.GetField("__interceptors"); ImplementGetObjectData(@class); ImplementProxyTargetAccessor(@class, interceptors); foreach (var attribute in targetType.GetTypeInfo().GetNonInheritableAttributes()) { @class.DefineCustomAttribute(attribute.Builder); } }
/// <summary> /// Generates the class defined by the provided class emitter. /// </summary> /// <param name="class"> /// The <see cref="Castle.DynamicProxy.Generators.Emitters.ClassEmitter"/> /// being used to build the target type. /// </param> /// <param name="options">The options to use during proxy generation.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown if <paramref name="class" /> is <see langword="null" />. /// </exception> /// <remarks> /// <para> /// This overridden version of the method does everything that the base /// <see cref="Castle.DynamicProxy.Contributors.ProxyInstanceContributor.Generate"/> /// method does but it skips the part where it checks for non-inherited /// attributes and copies them over from the proxy target. /// </para> /// </remarks> public override void Generate(ClassEmitter @class, ProxyGenerationOptions options) { if (@class == null) { throw new ArgumentNullException("class"); } FieldReference field = @class.GetField("__interceptors"); this.ImplementGetObjectData(@class); this.ImplementProxyTargetAccessor(@class, field); }
protected virtual Type Init(string typeName, out ClassEmitter emitter, Type proxyTargetType, out FieldReference interceptorsField, IEnumerable <Type> interfaces) { Type baseType = ProxyGenerationOptions.BaseTypeForInterfaceProxy; emitter = BuildClassEmitter(typeName, baseType, interfaces); CreateFields(emitter, proxyTargetType); CreateTypeAttributes(emitter); interceptorsField = emitter.GetField("__interceptors"); return(baseType); }
public virtual void Generate(ClassEmitter @class, ProxyGenerationOptions options) { var interceptors = @class.GetField("__interceptors"); //#if FEATURE_SERIALIZATION ImplementGetObjectData(@class); //#endif ImplementProxyTargetAccessor(@class, interceptors); foreach (var attribute in targetType.GetNonInheritableAttributes()) { @class.DefineCustomAttribute(attribute); } }
protected override void CreateFields(ClassEmitter emitter) { base.CreateFields(emitter); var interceptorsField = emitter.GetField(InterceptorsFieldName); if (interceptorsField != null) { emitter.DefineCustomAttributeFor <IgnoreDataMemberAttribute>(interceptorsField); CreateMetadataProperty(emitter, interceptorsField); } }
public virtual void Generate(ClassEmitter @class, ProxyGenerationOptions options) { var interceptors = @class.GetField("__interceptors"); #if !SILVERLIGHT ImplementGetObjectData(@class); #endif ImplementProxyTargetAccessor(@class, interceptors); foreach (var attribute in AttributeUtil.GetNonInheritableAttributes(targetType)) { @class.DefineCustomAttribute(attribute); } }
protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod) { var invocation = GetInvocationType(method, @class, options); return(new MethodWithInvocationGenerator(method, @class.GetField("__interceptors"), invocation, (c, m) => c.GetField("__target").ToExpression(), overrideMethod, null)); }
protected override MethodGenerator GetMethodGenerator( MetaMethod method, ClassEmitter @class, OverrideMethodDelegate overrideMethod ) { if (method.Ignore) { return(null); } var methodIsDirectlyAccessible = IsDirectlyAccessible(method); if (!method.Proxyable) { if (methodIsDirectlyAccessible) { return(new ForwardingMethodGenerator( method, overrideMethod, (c, m) => c.GetField("__target") )); } else { return(IndirectlyCalledMethodGenerator( method, @class, overrideMethod, skipInterceptors: true )); } } if (!methodIsDirectlyAccessible) { return(IndirectlyCalledMethodGenerator(method, @class, overrideMethod)); } var invocation = GetInvocationType(method, @class); return(new MethodWithInvocationGenerator( method, @class.GetField("__interceptors"), invocation, (c, m) => c.GetField("__target"), overrideMethod, null )); }
protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod) { if (!method.Proxyable) { return(new MinimialisticMethodGenerator(method, overrideMethod)); } var invocation = GetInvocationType(method, @class, options); return(new MethodWithInvocationGenerator(method, @class.GetField("__interceptors"), invocation, getTargetExpression, overrideMethod, null)); }
protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options, CreateMethodDelegate createMethod) { if (!method.Proxyable) { return(new ForwardingMethodGenerator(method, createMethod, (c, m) => c.GetField("__target"))); } var invocation = GetInvocationType(method, @class, options); return(new MethodWithInvocationGenerator(method, @class.GetField("__interceptors"), invocation, (c, m) => c.GetField("__target").ToExpression(), createMethod)); }
public override void Generate(ClassEmitter @class) { var interceptors = @class.GetField("__interceptors"); #if FEATURE_SERIALIZATION if (implementISerializable) { ImplementGetObjectData(@class); Constructor(@class); } #endif ImplementProxyTargetAccessor(@class, interceptors); foreach (var attribute in targetType.GetNonInheritableAttributes()) { @class.DefineCustomAttribute(attribute.Builder); } }
public override void Generate(ClassEmitter @class, ProxyGenerationOptions options) { var interceptors = @class.GetField("__interceptors"); #if !SILVERLIGHT if (implementISerializable) { ImplementGetObjectData(@class); Constructor(@class); } #endif ImplementProxyTargetAccessor(@class, interceptors); foreach (var attribute in targetType.GetNonInheritableAttributes()) { @class.DefineCustomAttribute(attribute); } }
private void ImplementChangeProxyTarget(AbstractTypeEmitter invocation, ClassEmitter @class) { var changeInvocationTarget = invocation.CreateMethod("ChangeProxyTarget", typeof(void), new[] { typeof(object) }); changeInvocationTarget.CodeBuilder.AddStatement( new ExpressionStatement( new ConvertExpression(@class.TypeBuilder, new FieldReference(InvocationMethods.ProxyObject).ToExpression()))); var field = @class.GetField("__target"); changeInvocationTarget.CodeBuilder.AddStatement( new AssignStatement( new FieldReference(field.Reference) { OwnerReference = null }, new ConvertExpression(field.Fieldbuilder.FieldType, changeInvocationTarget.Arguments[0].ToExpression()))); changeInvocationTarget.CodeBuilder.AddStatement(new ReturnStatement()); }
protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options, CreateMethodDelegate createMethod) { if (methodsToSkip.Contains(method.Method)) { return(null); } if (!method.Proxyable) { return(new MinimialisticMethodGenerator(method, createMethod)); } var invocation = GetInvocationType(method, @class, options); return(new MethodWithInvocationGenerator(method, @class.GetField("__interceptors"), invocation, (c, m) => new TypeTokenExpression(targetType), createMethod)); }
private void ImplementChangeProxyTarget(AbstractTypeEmitter invocation, ClassEmitter @class) { #if !NETFX_CORE var changeInvocationTarget = invocation.CreateMethod("ChangeProxyTarget", typeof(void), new[] { typeof(object) }); changeInvocationTarget.CodeBuilder.AddStatement( new ExpressionStatement( new ConvertExpression(@class.TypeBuilder, new FieldReference(InvocationMethods.ProxyObject.AsFieldInfo()).ToExpression()))); var field = @class.GetField("__target"); changeInvocationTarget.CodeBuilder.AddStatement( new AssignStatement( new FieldReference(field.Reference) { OwnerReference = null }, new ConvertExpression(field.Fieldbuilder.FieldType, changeInvocationTarget.Arguments[0].ToExpression()))); changeInvocationTarget.CodeBuilder.AddStatement(new ReturnStatement()); #else throw new NotImplementedException("This operation has not been implemented."); #endif }
private Reference GetTarget(ClassEmitter @class, MethodInfo method) { return(new AsTypeReference(@class.GetField("__target"), method.DeclaringType)); }
protected void ImplementGetObjectData(ClassEmitter emitter) { var getObjectData = emitter.CreateMethod( "GetObjectData", typeof(void), new[] { typeof(SerializationInfo), typeof(StreamingContext) } ); var info = getObjectData.Arguments[0]; var typeLocal = getObjectData.CodeBuilder.DeclareLocal(typeof(Type)); getObjectData.CodeBuilder.AddStatement( new AssignStatement( typeLocal, new MethodInvocationExpression( null, TypeMethods.StaticGetType, new LiteralStringExpression( typeof(ProxyObjectReference).AssemblyQualifiedName ), new LiteralBoolExpression(true), new LiteralBoolExpression(false) ) ) ); getObjectData.CodeBuilder.AddStatement( new MethodInvocationExpression(info, SerializationInfoMethods.SetType, typeLocal) ); foreach (var field in emitter.GetAllFields()) { if (field.Reference.IsStatic) { continue; } if (field.Reference.IsNotSerialized) { continue; } AddAddValueInvocation(info, getObjectData, field); } var interfacesLocal = getObjectData.CodeBuilder.DeclareLocal(typeof(string[])); getObjectData.CodeBuilder.AddStatement( new AssignStatement( interfacesLocal, new NewArrayExpression(interfaces.Length, typeof(string)) ) ); for (var i = 0; i < interfaces.Length; i++) { getObjectData.CodeBuilder.AddStatement( new AssignArrayStatement( interfacesLocal, i, new LiteralStringExpression(interfaces[i].AssemblyQualifiedName) ) ); } getObjectData.CodeBuilder.AddStatement( new MethodInvocationExpression( info, SerializationInfoMethods.AddValue_Object, new LiteralStringExpression("__interfaces"), interfacesLocal ) ); getObjectData.CodeBuilder.AddStatement( new MethodInvocationExpression( info, SerializationInfoMethods.AddValue_Object, new LiteralStringExpression("__baseType"), new LiteralStringExpression(emitter.BaseType.AssemblyQualifiedName) ) ); getObjectData.CodeBuilder.AddStatement( new MethodInvocationExpression( info, SerializationInfoMethods.AddValue_Object, new LiteralStringExpression("__proxyGenerationOptions"), emitter.GetField("proxyGenerationOptions") ) ); getObjectData.CodeBuilder.AddStatement( new MethodInvocationExpression( info, SerializationInfoMethods.AddValue_Object, new LiteralStringExpression("__proxyTypeId"), new LiteralStringExpression(proxyTypeId) ) ); CustomizeGetObjectData( getObjectData.CodeBuilder, info, getObjectData.Arguments[1], emitter ); getObjectData.CodeBuilder.AddStatement(new ReturnStatement()); }
protected override Reference GetTargetReference(ClassEmitter emitter) { return(emitter.GetField("__target")); }
protected override Expression GetTargetReferenceExpression(ClassEmitter emitter) { return(emitter.GetField("__target").ToExpression()); }
protected void ImplementGetObjectData(ClassEmitter emitter) { var serializationInfo = new ArgumentReference(typeof(SerializationInfo)); var streamingContext = new ArgumentReference(typeof(StreamingContext)); MethodEmitter getObjectData = emitter.CreateMethod("GetObjectData", serializationInfo, streamingContext); LocalReference typeLocal = getObjectData.CodeBuilder.DeclareLocal(typeof(Type)); getObjectData.CodeBuilder.AddStatement( new AssignStatement( typeLocal, new MethodInvocationExpression( null, TypeMethods.StaticGetType, new ConstReference(typeof(ProxyObjectReference).AssemblyQualifiedName).ToExpression(), new ConstReference(1).ToExpression(), new ConstReference(0).ToExpression()))); getObjectData.CodeBuilder.AddStatement( new ExpressionStatement( new MethodInvocationExpression( serializationInfo, SerializationInfoMethods.SetType, typeLocal.ToExpression()))); foreach (var field in emitter.GetAllFields()) { if (field.Reference.IsStatic) { continue; } if (field.Reference.IsNotSerialized) { continue; } AddAddValueInvocation(serializationInfo, getObjectData, field); } LocalReference interfacesLocal = getObjectData.CodeBuilder.DeclareLocal(typeof(string[])); getObjectData.CodeBuilder.AddStatement( new AssignStatement( interfacesLocal, new NewArrayExpression(interfaces.Length, typeof(string)))); for (int i = 0; i < interfaces.Length; i++) { getObjectData.CodeBuilder.AddStatement( new AssignArrayStatement( interfacesLocal, i, new ConstReference(interfaces[i].AssemblyQualifiedName).ToExpression())); } getObjectData.CodeBuilder.AddStatement( new ExpressionStatement( new MethodInvocationExpression( serializationInfo, SerializationInfoMethods.AddValue_Object, new ConstReference("__interfaces").ToExpression(), interfacesLocal.ToExpression()))); getObjectData.CodeBuilder.AddStatement( new ExpressionStatement( new MethodInvocationExpression( serializationInfo, SerializationInfoMethods.AddValue_Object, new ConstReference("__baseType").ToExpression(), new ConstReference(emitter.BaseType.AssemblyQualifiedName).ToExpression()))); getObjectData.CodeBuilder.AddStatement( new ExpressionStatement( new MethodInvocationExpression( serializationInfo, SerializationInfoMethods.AddValue_Object, new ConstReference("__proxyGenerationOptions").ToExpression(), emitter.GetField("proxyGenerationOptions").ToExpression()))); getObjectData.CodeBuilder.AddStatement( new ExpressionStatement( new MethodInvocationExpression(serializationInfo, SerializationInfoMethods.AddValue_Object, new ConstReference("__proxyTypeId").ToExpression(), new ConstReference(proxyTypeId).ToExpression()))); CustomizeGetObjectData(getObjectData.CodeBuilder, serializationInfo, streamingContext, emitter); getObjectData.CodeBuilder.AddStatement(new ReturnStatement()); }