Skip to content

Commit

Permalink
ProxyInstanceContributor -> SerializableContributor
Browse files Browse the repository at this point in the history
as its sole remaining function is to implement `ISerializable`.
  • Loading branch information
stakx committed Jan 5, 2021
1 parent 05baeb1 commit 12f6964
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,48 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if FEATURE_SERIALIZATION

namespace Castle.DynamicProxy.Contributors
{
using System;
using System.Collections.Generic;
using System.Reflection;
#if FEATURE_SERIALIZATION
using System.Runtime.Serialization;
#endif

using Castle.DynamicProxy.Generators.Emitters;
using Castle.DynamicProxy.Generators.Emitters.CodeBuilders;
using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
using Castle.DynamicProxy.Internal;
using Castle.DynamicProxy.Tokens;

internal class ClassProxyInstanceContributor : ProxyInstanceContributor
internal class ClassProxySerializableContributor : SerializableContributor
{
#if FEATURE_SERIALIZATION
private readonly bool delegateToBaseGetObjectData;
private readonly bool implementISerializable;
private ConstructorInfo serializationConstructor;
private readonly IList<FieldReference> serializedFields = new List<FieldReference>();
#endif

public ClassProxyInstanceContributor(Type targetType, IList<MethodInfo> methodsToSkip, Type[] interfaces,
public ClassProxySerializableContributor(Type targetType, IList<MethodInfo> methodsToSkip, Type[] interfaces,
string typeId)
: base(targetType, interfaces, typeId)
{
#if FEATURE_SERIALIZATION
if (targetType.IsSerializable)
{
implementISerializable = true;
delegateToBaseGetObjectData = VerifyIfBaseImplementsGetObjectData(targetType, methodsToSkip);
}
#endif
}

public override void Generate(ClassEmitter @class)
{
#if FEATURE_SERIALIZATION
if (implementISerializable)
{
ImplementGetObjectData(@class);
Constructor(@class);
}
#endif
}

#if FEATURE_SERIALIZATION
protected override void AddAddValueInvocation(ArgumentReference serializationInfo, MethodEmitter getObjectData,
FieldReference field)
{
Expand Down Expand Up @@ -218,6 +211,7 @@ private bool VerifyIfBaseImplementsGetObjectData(Type baseType, IList<MethodInfo

return true;
}
#endif
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if FEATURE_SERIALIZATION

namespace Castle.DynamicProxy.Contributors
{
using System;
Expand All @@ -21,14 +23,13 @@ namespace Castle.DynamicProxy.Contributors
using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
using Castle.DynamicProxy.Tokens;

internal class InterfaceProxyInstanceContributor : ProxyInstanceContributor
internal class InterfaceProxySerializableContributor : SerializableContributor
{
public InterfaceProxyInstanceContributor(Type targetType, string proxyGeneratorId, Type[] interfaces)
public InterfaceProxySerializableContributor(Type targetType, string proxyGeneratorId, Type[] interfaces)
: base(targetType, interfaces, proxyGeneratorId)
{
}

#if FEATURE_SERIALIZATION
protected override void CustomizeGetObjectData(AbstractCodeBuilder codebuilder, ArgumentReference serializationInfo,
ArgumentReference streamingContext, ClassEmitter emitter)
{
Expand All @@ -47,6 +48,7 @@ public InterfaceProxyInstanceContributor(Type targetType, string proxyGeneratorI
new ConstReference(targetType.AssemblyQualifiedName).
ToExpression())));
}
#endif
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,27 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if FEATURE_SERIALIZATION

namespace Castle.DynamicProxy.Contributors
{
using System;
using System.Reflection;
#if FEATURE_SERIALIZATION
using System.Runtime.Serialization;
#endif

using Castle.DynamicProxy.Generators;
using Castle.DynamicProxy.Generators.Emitters;
using Castle.DynamicProxy.Generators.Emitters.CodeBuilders;
using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
using Castle.DynamicProxy.Internal;
#if FEATURE_SERIALIZATION
using Castle.DynamicProxy.Serialization;
#endif
using Castle.DynamicProxy.Tokens;

internal abstract class ProxyInstanceContributor : ITypeContributor
internal abstract class SerializableContributor : ITypeContributor
{
protected readonly Type targetType;
private readonly string proxyTypeId;
private readonly Type[] interfaces;

protected ProxyInstanceContributor(Type targetType, Type[] interfaces, string proxyTypeId)
protected SerializableContributor(Type targetType, Type[] interfaces, string proxyTypeId)
{
this.targetType = targetType;
this.proxyTypeId = proxyTypeId;
Expand All @@ -45,12 +41,9 @@ protected ProxyInstanceContributor(Type targetType, Type[] interfaces, string pr

public virtual void Generate(ClassEmitter @class)
{
#if FEATURE_SERIALIZATION
ImplementGetObjectData(@class);
#endif
}

#if FEATURE_SERIALIZATION
protected void ImplementGetObjectData(ClassEmitter emitter)
{
var getObjectData = emitter.CreateMethod("GetObjectData", typeof(void),
Expand Down Expand Up @@ -156,10 +149,11 @@ protected void ImplementGetObjectData(ClassEmitter emitter)

protected abstract void CustomizeGetObjectData(AbstractCodeBuilder builder, ArgumentReference serializationInfo,
ArgumentReference streamingContext, ClassEmitter emitter);
#endif

public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model)
{
}
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ protected BaseClassProxyGenerator(ModuleScope scope, Type targetType, Type[] int

protected abstract FieldReference TargetField { get; }

protected abstract ProxyInstanceContributor GetProxyInstanceContributor(List<MethodInfo> methodsToSkip);
#if FEATURE_SERIALIZATION
protected abstract SerializableContributor GetSerializableContributor(List<MethodInfo> methodsToSkip);
#endif

protected abstract CompositeTypeContributor GetProxyTargetContributor(List<MethodInfo> methodsToSkip, INamingScope namingScope);

Expand Down Expand Up @@ -178,12 +180,12 @@ private IEnumerable<Type> GetTypeImplementerMapping(out IEnumerable<ITypeContrib

// 4. plus special interfaces

var instanceContributor = GetProxyInstanceContributor(methodsToSkip);
contributorsList.Add(instanceContributor);
#if FEATURE_SERIALIZATION
if (targetType.IsSerializable)
{
AddMappingForISerializable(typeImplementerMapping, instanceContributor);
var serializableContributor = GetSerializableContributor(methodsToSkip);
contributorsList.Add(serializableContributor);
AddMappingForISerializable(typeImplementerMapping, serializableContributor);
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ protected override Type GenerateType(string typeName, INamingScope namingScope)

// 4. plus special interfaces

var instanceContributor = new InterfaceProxyInstanceContributor(targetType, GeneratorType, interfaces);
contributorsList.Add(instanceContributor);
#if FEATURE_SERIALIZATION
AddMappingForISerializable(typeImplementerMapping, instanceContributor);
var serializableContributor = new InterfaceProxySerializableContributor(targetType, GeneratorType, interfaces);
contributorsList.Add(serializableContributor);
AddMappingForISerializable(typeImplementerMapping, serializableContributor);
#endif

var proxyTargetAccessorContributor = GetProxyTargetAccessorContributor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ protected override CacheKey GetCacheKey()
return new CacheKey(targetType, interfaces, ProxyGenerationOptions);
}

protected override ProxyInstanceContributor GetProxyInstanceContributor(List<MethodInfo> methodsToSkip)
#if FEATURE_SERIALIZATION
protected override SerializableContributor GetSerializableContributor(List<MethodInfo> methodsToSkip)
{
return new ClassProxyInstanceContributor(targetType, methodsToSkip, interfaces, ProxyTypeConstants.Class);
return new ClassProxySerializableContributor(targetType, methodsToSkip, interfaces, ProxyTypeConstants.Class);
}
#endif

protected override CompositeTypeContributor GetProxyTargetContributor(List<MethodInfo> methodsToSkip, INamingScope namingScope)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ protected override void CreateFields(ClassEmitter emitter)
CreateTargetField(emitter);
}

protected override ProxyInstanceContributor GetProxyInstanceContributor(List<MethodInfo> methodsToSkip)
#if FEATURE_SERIALIZATION
protected override SerializableContributor GetSerializableContributor(List<MethodInfo> methodsToSkip)
{
return new ClassProxyInstanceContributor(targetType, methodsToSkip, interfaces, ProxyTypeConstants.ClassWithTarget);
return new ClassProxySerializableContributor(targetType, methodsToSkip, interfaces, ProxyTypeConstants.ClassWithTarget);
}
#endif

protected override CompositeTypeContributor GetProxyTargetContributor(List<MethodInfo> methodsToSkip, INamingScope namingScope)
{
Expand Down

0 comments on commit 12f6964

Please sign in to comment.