public void GetOverrideInterfaceMethodsByMixinMethod() { var importer = new AttributeBasedMetadataImporter(); var identifier = new ConcreteMixinTypeIdentifier(typeof(MixinWithAbstractMembers), new HashSet <MethodInfo> (), new HashSet <MethodInfo> ()); var interfaceType = typeof(LoadableConcreteMixinTypeForMixinWithAbstractMembers.IOverriddenMethods); var result = importer.GetOverrideInterfaceMethodsByMixinMethod(interfaceType, identifier); var ifcMethod1 = interfaceType.GetMethod("AbstractMethod"); var mixinMethod1 = identifier.MixinType.GetMethod("AbstractMethod", BindingFlags.NonPublic | BindingFlags.Instance); var ifcMethod2 = interfaceType.GetMethod("get_AbstractProperty"); var mixinMethod2 = identifier.MixinType.GetMethod("get_AbstractProperty", BindingFlags.NonPublic | BindingFlags.Instance); var ifcMethod3 = interfaceType.GetMethod("add_AbstractEvent"); var mixinMethod3 = identifier.MixinType.GetMethod("add_AbstractEvent", BindingFlags.NonPublic | BindingFlags.Instance); var ifcMethod4 = interfaceType.GetMethod("remove_AbstractEvent"); var mixinMethod4 = identifier.MixinType.GetMethod("remove_AbstractEvent", BindingFlags.NonPublic | BindingFlags.Instance); var ifcMethod5 = interfaceType.GetMethod("RaiseEvent"); var mixinMethod5 = identifier.MixinType.GetMethod("RaiseEvent", BindingFlags.NonPublic | BindingFlags.Instance); var expected = new Dictionary <MethodInfo, MethodInfo> { { mixinMethod1, ifcMethod1 }, { mixinMethod2, ifcMethod2 }, { mixinMethod3, ifcMethod3 }, { mixinMethod4, ifcMethod4 }, { mixinMethod5, ifcMethod5 }, }; Assert.That(result, Is.EquivalentTo(expected)); }
public void GetMetadataForMixinType_Wrappers() { var importerMock = new MockRepository().PartialMock <AttributeBasedMetadataImporter> (); var expectedIdentifier = new ConcreteMixinTypeIdentifier(typeof(object), new HashSet <MethodInfo> (), new HashSet <MethodInfo> ()); var method1 = ReflectionObjectMother.GetSomeNonPublicMethod(); var method2 = typeof(DateTime).GetMethod("get_InternalTicks", BindingFlags.NonPublic | BindingFlags.Instance); var wrapper1 = typeof(DateTime).GetMethod("get_Month"); var wrapper2 = typeof(DateTime).GetMethod("get_Year"); SetupImporterMock( importerMock, expectedIdentifier, new Dictionary <MethodInfo, MethodInfo> (), new Dictionary <MethodInfo, MethodInfo> { { method1, wrapper1 }, { method2, wrapper2 } }); importerMock.Replay(); var result = importerMock.GetMetadataForMixinType(typeof(LoadableConcreteMixinTypeForMixinWithAbstractMembers)); Assert.That(result.GetPubliclyCallableMixinMethod(method1), Is.EqualTo(wrapper1)); Assert.That(result.GetPubliclyCallableMixinMethod(method2), Is.EqualTo(wrapper2)); importerMock.VerifyAllExpectations(); }
// Always remember: the whole configuration must be serialized as one single, flat object (or SerializationInfo), we cannot rely on any // nested objects to be deserialized in the right order public static void GetObjectDataForGeneratedTypes( SerializationInfo info, StreamingContext context, object mixin, ConcreteMixinTypeIdentifier identifier, bool serializeBaseMembers, string pipelineIdentifier) { ArgumentUtility.CheckNotNull("info", info); ArgumentUtility.CheckNotNull("mixin", mixin); ArgumentUtility.CheckNotNull("identifier", identifier); info.SetType(typeof(MixinSerializationHelper)); var identifierSerializer = new SerializationInfoConcreteMixinTypeIdentifierSerializer(info, "__identifier"); identifier.Serialize(identifierSerializer); object[] baseMemberValues; if (serializeBaseMembers) { var baseType = mixin.GetType().BaseType; Assertion.IsNotNull(baseType, "Generated mixin types always have a base type."); MemberInfo[] baseMembers = FormatterServices.GetSerializableMembers(baseType); baseMemberValues = FormatterServices.GetObjectData(mixin, baseMembers); } else { baseMemberValues = null; } info.AddValue("__baseMemberValues", baseMemberValues); info.AddValue("__participantConfigurationID", pipelineIdentifier); }
public MixinSerializationHelper(SerializationInfo info, StreamingContext context) { ArgumentUtility.CheckNotNull("info", info); _context = context; var identifierDeserializer = new SerializationInfoConcreteMixinTypeIdentifierDeserializer(info, "__identifier"); var identifier = ConcreteMixinTypeIdentifier.Deserialize(identifierDeserializer); var pipelineIdentifier = info.GetString("__participantConfigurationID"); var pipeline = SafeServiceLocator.Current.GetInstance <IPipelineRegistry>().Get(pipelineIdentifier); var mixinType = pipeline.ReflectionService.GetAdditionalType(identifier); _baseMemberValues = (object[])info.GetValue("__baseMemberValues", typeof(object[])); // Usually, instantiate a deserialized object using GetSafeUninitializedObject. // However, _baseMemberValues being null means that the object itself manages its member deserialization via ISerializable. In such a case, we // need to use the deserialization constructor to instantiate the object. if (_baseMemberValues != null) { _deserializedObject = FormatterServices.GetSafeUninitializedObject(mixinType); } else { Assertion.IsTrue(typeof(ISerializable).IsAssignableFrom(mixinType)); _deserializedObject = Activator.CreateInstance( mixinType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { info, context }, null); } SerializationImplementer.RaiseOnDeserializing(_deserializedObject, _context); }
public void GetMetadataForMixinType_OverrideInterfaceMethods() { var importerMock = new MockRepository().PartialMock <AttributeBasedMetadataImporter> (); var expectedIdentifier = new ConcreteMixinTypeIdentifier(typeof(object), new HashSet <MethodInfo> (), new HashSet <MethodInfo> ()); var mixinMethod1 = typeof(DateTime).GetMethod("get_Now"); var mixinMethod2 = typeof(DateTime).GetMethod("get_Day"); var ifcMethod1 = typeof(DateTime).GetMethod("get_Month"); var ifcMethod2 = typeof(DateTime).GetMethod("get_Year"); SetupImporterMock( importerMock, expectedIdentifier, new Dictionary <MethodInfo, MethodInfo> { { mixinMethod1, ifcMethod1 }, { mixinMethod2, ifcMethod2 } }, new Dictionary <MethodInfo, MethodInfo> ()); importerMock.Replay(); var result = importerMock.GetMetadataForMixinType(typeof(LoadableConcreteMixinTypeForMixinWithAbstractMembers)); Assert.That(result.GetOverrideInterfaceMethod(mixinMethod1), Is.SameAs(ifcMethod1)); Assert.That(result.GetOverrideInterfaceMethod(mixinMethod2), Is.SameAs(ifcMethod2)); importerMock.VerifyAllExpectations(); }
public ConcreteMixinTypeIdentifier GetConcreteMixinTypeIdentifier() { if (_concreteTypeIdentifier == null) { _concreteTypeIdentifier = CalculateConcreteTypeIdentifier(); } return(_concreteTypeIdentifier); }
public void GetConcreteMixinTypeIdentifier_NoOverrides() { var expectedIdentifier = new ConcreteMixinTypeIdentifier(typeof(NullMixin), new HashSet <MethodInfo>(), new HashSet <MethodInfo> ()); var definition = DefinitionObjectMother.BuildUnvalidatedDefinition(typeof(NullTarget), typeof(NullMixin)).Mixins[0]; Assert.That(definition.GetConcreteMixinTypeIdentifier(), Is.EqualTo(expectedIdentifier)); }
public void Create_Identifier() { var attribute = ConcreteMixinTypeAttribute.Create(_simpleIdentifier); var deserializer = new AttributeConcreteMixinTypeIdentifierDeserializer(attribute.ConcreteMixinTypeIdentifierData); Assert.That(ConcreteMixinTypeIdentifier.Deserialize(deserializer), Is.EqualTo(_simpleIdentifier)); }
public void GetObjectDataForGeneratedTypes_SerializesIdentifier() { CallGetObjectDataForGeneratedTypes(true); var identifierDeserializer = new SerializationInfoConcreteMixinTypeIdentifierDeserializer(_serializationInfo, "__identifier"); var deserializedIdentifier = ConcreteMixinTypeIdentifier.Deserialize(identifierDeserializer); Assert.That(deserializedIdentifier, Is.EqualTo(_identifier)); }
public void AddConcreteMixinTypeAttribute(IMutableMember member, ConcreteMixinTypeIdentifier concreteMixinTypeIdentifier) { ArgumentUtility.CheckNotNull("member", member); ArgumentUtility.CheckNotNull("concreteMixinTypeIdentifier", concreteMixinTypeIdentifier); var attributeData = ConcreteMixinTypeAttribute.Create(concreteMixinTypeIdentifier).ConcreteMixinTypeIdentifierData; var attribute = new CustomAttributeDeclaration(s_concreteMixinTypeAttributeConstructor, new object[] { attributeData }); member.AddCustomAttribute(attribute); }
public void GetMetadataForMixinType_NoOverrideInterface() { var importerMock = new MockRepository().PartialMock <AttributeBasedMetadataImporter> (); var expectedIdentifier = new ConcreteMixinTypeIdentifier(typeof(object), new HashSet <MethodInfo> (), new HashSet <MethodInfo> ()); importerMock.Expect(mock => mock.GetIdentifierForMixinType(typeof(object))).Return(expectedIdentifier); importerMock.Replay(); importerMock.GetMetadataForMixinType(typeof(object)); }
public void SetUp() { _serializationInfo = new SerializationInfo(typeof(object), new FormatterConverter()); _context = new StreamingContext(); _concreteMixin = new FakeConcreteMixinType(); var classContext = ClassContextObjectMother.Create(typeof(ClassOverridingMixinMembers), typeof(FakeConcreteMixinType)); _identifier = DefinitionObjectMother.GetTargetClassDefinition(classContext).Mixins[0].GetConcreteMixinTypeIdentifier(); _pipeline = SafeServiceLocator.Current.GetInstance <IPipelineFactory>().Create("MixinSerializationHelper", new MixinParticipant()); SafeServiceLocator.Current.GetInstance <IPipelineRegistry>().Register(_pipeline); }
public MixinTypeGenerator(ConcreteMixinTypeIdentifier identifier, MutableType type, IAttributeGenerator attributeGenerator, string pipelineIdentifier) { ArgumentUtility.CheckNotNull("identifier", identifier); ArgumentUtility.CheckNotNull("type", type); ArgumentUtility.CheckNotNull("attributeGenerator", attributeGenerator); ArgumentUtility.CheckNotNullOrEmpty("pipelineIdentifier", pipelineIdentifier); _identifier = identifier; _type = type; _attributeGenerator = attributeGenerator; _pipelineIdentifier = pipelineIdentifier; }
public void GetMetadataForMixinType_GeneratedOverrideInterface() { var importerMock = new MockRepository().PartialMock <AttributeBasedMetadataImporter> (); var expectedIdentifier = new ConcreteMixinTypeIdentifier(typeof(object), new HashSet <MethodInfo> (), new HashSet <MethodInfo> ()); SetupImporterMock(importerMock, expectedIdentifier, new Dictionary <MethodInfo, MethodInfo> (), new Dictionary <MethodInfo, MethodInfo>()); importerMock.Replay(); var result = importerMock.GetMetadataForMixinType(typeof(LoadableConcreteMixinTypeForMixinWithAbstractMembers)); Assert.That(result.GeneratedOverrideInterface, Is.SameAs(typeof(LoadableConcreteMixinTypeForMixinWithAbstractMembers.IOverriddenMethods))); importerMock.VerifyAllExpectations(); }
public void GetConcreteMixinTypeIdentifier_Overridden_TypeOverridesMethod() { var overridden = typeof(MixinWithMethodsOverriddenByDifferentClasses).GetMethod("M1"); var expectedIdentifier = new ConcreteMixinTypeIdentifier( typeof(MixinWithMethodsOverriddenByDifferentClasses), new HashSet <MethodInfo> (), new HashSet <MethodInfo> { overridden }); var definition = DefinitionObjectMother .GetActiveTargetClassDefinition(typeof(DerivedClassOverridingMixinMethod)) .Mixins[typeof(MixinWithMethodsOverriddenByDifferentClasses)]; Assert.That(definition.GetConcreteMixinTypeIdentifier(), Is.EqualTo(expectedIdentifier)); }
public void GetIdentifierForMixinType() { var typeMock = MockRepository.GenerateMock <_Type> (); var identifier = new ConcreteMixinTypeIdentifier(typeof(object), new HashSet <MethodInfo>(), new HashSet <MethodInfo>()); var attribute = ConcreteMixinTypeAttribute.Create(identifier); typeMock.Expect(mock => mock.GetCustomAttributes(typeof(ConcreteMixinTypeAttribute), false)).Return(new[] { attribute }); typeMock.Replay(); var importer = new AttributeBasedMetadataImporter(); var result = importer.GetIdentifierForMixinType(typeMock); Assert.That(result, Is.EqualTo(identifier)); typeMock.VerifyAllExpectations(); }
public void GetConcreteMixinTypeIdentifier_Overriders() { var overrider = typeof(MixinOverridingToString).GetMethod("ToString"); var expectedIdentifier = new ConcreteMixinTypeIdentifier( typeof(MixinOverridingToString), new HashSet <MethodInfo> { overrider }, new HashSet <MethodInfo> ()); var definition = DefinitionObjectMother. BuildUnvalidatedDefinition(typeof(object), typeof(MixinOverridingToString)) .Mixins[typeof(MixinOverridingToString)]; Assert.That(definition.GetConcreteMixinTypeIdentifier(), Is.EqualTo(expectedIdentifier)); }
public void Serialize() { var overriders = new HashSet <MethodInfo> { _overrider1, _overrider2 }; var overridden = new HashSet <MethodInfo> { _overridden1, _overridden2 }; var identifier = new ConcreteMixinTypeIdentifier(typeof(BT1Mixin1), overriders, overridden); var serializerMock = MockRepository.GenerateMock <IConcreteMixinTypeIdentifierSerializer> (); identifier.Serialize(serializerMock); serializerMock.AssertWasCalled(mock => mock.AddMixinType(typeof(BT1Mixin1))); serializerMock.AssertWasCalled(mock => mock.AddOverriders(overriders)); serializerMock.AssertWasCalled(mock => mock.AddOverridden(overridden)); }
public void SetUp() { var identifier = new ConcreteMixinTypeIdentifier(typeof(object), new HashSet <MethodInfo> (), new HashSet <MethodInfo> ()); _nonPublicMethod = ReflectionObjectMother.GetSomeNonPublicMethod(); _publicMethod = ReflectionObjectMother.GetSomePublicMethod(); _wrapperOrInterfaceMethod = ReflectionObjectMother.GetSomeMethod(); _concreteMixinType = new ConcreteMixinType( identifier, typeof(object), typeof(IServiceProvider), new Dictionary <MethodInfo, MethodInfo> { { _nonPublicMethod, _wrapperOrInterfaceMethod } }, new Dictionary <MethodInfo, MethodInfo> { { _nonPublicMethod, _wrapperOrInterfaceMethod } }); }
private void SetupImporterMock( AttributeBasedMetadataImporter importerMock, ConcreteMixinTypeIdentifier expectedIdentifier, Dictionary <MethodInfo, MethodInfo> overrideInterfaceMethods, Dictionary <MethodInfo, MethodInfo> methodWrappers) { importerMock .Expect(mock => mock.GetIdentifierForMixinType(typeof(LoadableConcreteMixinTypeForMixinWithAbstractMembers))) .Return(expectedIdentifier); importerMock .Expect(mock => mock.GetOverrideInterfaceMethodsByMixinMethod( typeof(LoadableConcreteMixinTypeForMixinWithAbstractMembers.IOverriddenMethods), expectedIdentifier)) .Return(overrideInterfaceMethods); importerMock .Expect(mock => mock.GetMethodWrappersForMixinType(typeof(LoadableConcreteMixinTypeForMixinWithAbstractMembers))) .Return(methodWrappers); }
public void IntegrationTest_GenericMethods() { var referenceIdentifier = new ConcreteMixinTypeIdentifier( typeof(BT1Mixin1), new HashSet <MethodInfo> { _genericMethod }, new HashSet <MethodInfo> { _genericMethod }); var serializer = new ExpressionConcreteMixinTypeIdentifierSerializer(); referenceIdentifier.Serialize(serializer); object result = BuildTypeAndInvokeMethod(serializer.CreateExpression()); Assert.That(result, Is.EqualTo(referenceIdentifier)); }
public void GetConcreteMixinTypeIdentifier_ProtectedOverriders() { const BindingFlags bf = BindingFlags.NonPublic | BindingFlags.Instance; var overriders = new[] { typeof(MixinWithProtectedOverrider).GetMethod("VirtualMethod", bf), typeof(MixinWithProtectedOverrider).GetMethod("get_VirtualProperty", bf), typeof(MixinWithProtectedOverrider).GetMethod("add_VirtualEvent", bf), typeof(MixinWithProtectedOverrider).GetMethod("remove_VirtualEvent", bf), }; var expectedIdentifier = new ConcreteMixinTypeIdentifier( typeof(MixinWithProtectedOverrider), new HashSet <MethodInfo> (overriders), new HashSet <MethodInfo> ()); var definition = DefinitionObjectMother.BuildUnvalidatedDefinition(typeof(BaseType1), typeof(MixinWithProtectedOverrider)) .Mixins[typeof(MixinWithProtectedOverrider)]; Assert.That(definition.GetConcreteMixinTypeIdentifier(), Is.EqualTo(expectedIdentifier)); }
public void Equals_True_OrderOfWrappedProtectedMembersIsIrrelevant() { var one = new ConcreteMixinTypeIdentifier( typeof(BT1Mixin1), new HashSet <MethodInfo> { _overrider1, _overrider2 }, new HashSet <MethodInfo> { _overridden1, _overridden2 }); var two = new ConcreteMixinTypeIdentifier( typeof(BT1Mixin1), new HashSet <MethodInfo> { _overrider1, _overrider2 }, new HashSet <MethodInfo> { _overridden2, _overridden1 }); Assert.That(one, Is.EqualTo(two)); }
public void Equals_True() { var one = new ConcreteMixinTypeIdentifier( typeof(BT1Mixin1), new HashSet <MethodInfo> { _overrider1, _overrider2 }, new HashSet <MethodInfo> { _overridden1, _overridden2 }); var two = new ConcreteMixinTypeIdentifier( typeof(BT1Mixin1), new HashSet <MethodInfo> { _overrider1, _overrider2 }, new HashSet <MethodInfo> { _overridden1, _overridden2 }); Assert.That(one, Is.EqualTo(two)); }
public void GetHashCode_EqualObjects_OrderOfExternalOverridersIrrelevant() { var one = new ConcreteMixinTypeIdentifier( typeof(BT1Mixin1), new HashSet <MethodInfo> { _overrider1, _overrider2 }, new HashSet <MethodInfo> { _overridden1, _overridden2 }); var two = new ConcreteMixinTypeIdentifier( typeof(BT1Mixin1), new HashSet <MethodInfo> { _overrider2, _overrider1 }, new HashSet <MethodInfo> { _overridden1, _overridden2 }); Assert.That(one.GetHashCode(), Is.EqualTo(two.GetHashCode())); }
public void Equals_False_DifferentWrappedProtectedMembers() { var one = new ConcreteMixinTypeIdentifier( typeof(BT1Mixin1), new HashSet <MethodInfo> { _overrider1, _overrider2 }, new HashSet <MethodInfo> { _overridden1 }); var two = new ConcreteMixinTypeIdentifier( typeof(BT1Mixin1), new HashSet <MethodInfo> { _overrider1, _overrider2 }, new HashSet <MethodInfo> { _overridden1, _overridden2 }); Assert.That(one, Is.Not.EqualTo(two)); }
public void Deserialize() { var overriders = new HashSet <MethodInfo> { _overrider1, _overrider2 }; var overridden = new HashSet <MethodInfo> { _overridden1, _overridden2 }; var deserializerMock = MockRepository.GenerateMock <IConcreteMixinTypeIdentifierDeserializer> (); deserializerMock.Expect(mock => mock.GetMixinType()).Return(typeof(BT1Mixin1)); deserializerMock.Expect(mock => mock.GetOverriders()).Return(overriders); deserializerMock.Expect(mock => mock.GetOverridden()).Return(overridden); deserializerMock.Replay(); var identifier = ConcreteMixinTypeIdentifier.Deserialize(deserializerMock); deserializerMock.VerifyAllExpectations(); Assert.That(identifier.MixinType, Is.SameAs(typeof(BT1Mixin1))); Assert.That(identifier.Overriders, Is.SameAs(overriders)); Assert.That(identifier.Overridden, Is.SameAs(overridden)); }
public void SetUp() { _simpleIdentifier = new ConcreteMixinTypeIdentifier(typeof(object), new HashSet <MethodInfo> (), new HashSet <MethodInfo> ()); }
public ConcreteMixinType GetOrGenerateConcreteMixinType(ITypeAssemblyContext context, ConcreteMixinTypeIdentifier concreteMixinTypeIdentifier) { ArgumentUtility.CheckNotNull("context", context); ArgumentUtility.CheckNotNull("concreteMixinTypeIdentifier", concreteMixinTypeIdentifier); var concreteMixinTypeCache = GetOrCreateConcreteMixinTypeCache(context.ParticipantState); ConcreteMixinType concreteMixinType; if (!concreteMixinTypeCache.TryGetValue(concreteMixinTypeIdentifier, out concreteMixinType)) { concreteMixinType = GenerateConcreteMixinType(context, concreteMixinTypeIdentifier); context.GenerationCompleted += generatedTypeContext => { var completedConcreteMixinType = concreteMixinType.SubstituteMutableReflectionObjects(generatedTypeContext); concreteMixinTypeCache.Add(concreteMixinTypeIdentifier, completedConcreteMixinType); }; } return(concreteMixinType); }
private ConcreteMixinType GenerateConcreteMixinType(ITypeAssemblyContext context, ConcreteMixinTypeIdentifier concreteMixinTypeIdentifier) { var mixinProxyType = context.CreateAddtionalProxyType(concreteMixinTypeIdentifier, concreteMixinTypeIdentifier.MixinType); var generator = new MixinTypeGenerator(concreteMixinTypeIdentifier, mixinProxyType, new AttributeGenerator(), context.ParticipantConfigurationID); generator.AddInterfaces(); generator.AddFields(); generator.AddTypeInitializer(); generator.ImplementGetObjectData(); generator.AddMixinTypeAttribute(); generator.AddDebuggerAttributes(); var overrideInterface = generator.GenerateOverrides(); var methodWrappers = generator.GenerateMethodWrappers(); return(new ConcreteMixinType( concreteMixinTypeIdentifier, mixinProxyType, overrideInterface.Type, overrideInterface.InterfaceMethodsByOverriddenMethods, methodWrappers)); }