internal IAdminPacketService Create() { IEnumerable <Type> packetTransformerTypes = new AssemblyTypeFinder(Assembly.GetExecutingAssembly(), $"{GetType().Namespace}.PacketTransformers") .WithTypeMatcher(new ClassTypeMatcher()) .WithTypeMatcher(ImplementsTypeMatcher.Create <IPacketTransformer>()) .Find(); IEnumerable <Type> messageTransformerTypes = new AssemblyTypeFinder(Assembly.GetExecutingAssembly(), $"{GetType().Namespace}.MessageTransformers") .WithTypeMatcher(new ClassTypeMatcher()) .WithTypeMatcher(ImplementsTypeMatcher.Create <IMessageTransformer>()) .Find(); IPacketTransformer[] packetTransformers = new IPacketTransformer[packetTransformerTypes.Count()]; IMessageTransformer[] messageTransformers = new IMessageTransformer[messageTransformerTypes.Count()]; for (int i = 0; i < packetTransformers.Length; ++i) { packetTransformers[i] = (IPacketTransformer)Activator.CreateInstance(packetTransformerTypes.ElementAt(i)); } for (int i = 0; i < messageTransformers.Length; ++i) { messageTransformers[i] = (IMessageTransformer)Activator.CreateInstance(messageTransformerTypes.ElementAt(i)); } return(new AdminPacketService(packetTransformers, messageTransformers)); }
public void FindOnlyTypesThatAreImplementingGivenInterface() { var matcher = new ImplementsTypeMatcher(typeof(IAnimal)); Type[] expectedTypes = new Type[] { typeof(Cat), typeof(Dog), typeof(IAlienAnimal) }; AssertTest(matcher, expectedTypes); }
public void BeAbleToMatchGenericParameterWhichIsConcreteType_WhenConcreteTypeWasPassed() { var matcher = new ImplementsTypeMatcher(typeof(IFurnitureBox <>), typeof(Chair)); Type[] expectedTypes = new Type[] { typeof(ChairBox) }; inputTypes.Add(typeof(ChairBox)); AssertTest(matcher, expectedTypes); }
public void BeAbleToFindGenericTypeWithArgumentThatIsAlsoGeneric_WhenInterfaceOfGenericArgumentWasPassed() { var matcher = new ImplementsTypeMatcher(typeof(IFurnitureBox <>), typeof(IFurniture)); Type[] expectedTypes = new Type[] { typeof(GenericFurnitureBox <>) }; inputTypes.Add(typeof(GenericFurnitureBox <>)); AssertTest(matcher, expectedTypes); }
private void AssertTest(ImplementsTypeMatcher matcher, Type[] expectedTypes) { foreach (var it in inputTypes) { if (matcher.IsMatching(it) && !expectedTypes.Contains(it)) { throw new Exception($"Matcher is matching {it} when it is not on the expected types list"); } else if (!matcher.IsMatching(it) && expectedTypes.Contains(it)) { throw new Exception($"Matcher is NOT matching {it} when it is on the expected types list"); } } }