diff --git a/NetArchTest - all projects.sln b/NetArchTest - all projects.sln index 4b96287..ec6e2f6 100644 --- a/NetArchTest - all projects.sln +++ b/NetArchTest - all projects.sln @@ -19,6 +19,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetArchTest.SampleRules", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetArchTest.SampleLibrary", "samples\NetArchTest.SampleLibrary\NetArchTest.SampleLibrary.csproj", "{7123A8AE-678D-4D14-82E5-EB5607ABDC4A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetArchTest.CrossAssemblyTest.A", "test\NetArchTest.CrossAssemblyTest.A\NetArchTest.CrossAssemblyTest.A.csproj", "{70B787F2-8B79-4AA5-8C73-26682A605B6B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetArchTest.CrossAssemblyTest.B", "test\NetArchTest.CrossAssemblyTest.B\NetArchTest.CrossAssemblyTest.B.csproj", "{28692D43-3E08-43E4-BCBB-5940B9F741C6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +49,14 @@ Global {7123A8AE-678D-4D14-82E5-EB5607ABDC4A}.Debug|Any CPU.Build.0 = Debug|Any CPU {7123A8AE-678D-4D14-82E5-EB5607ABDC4A}.Release|Any CPU.ActiveCfg = Release|Any CPU {7123A8AE-678D-4D14-82E5-EB5607ABDC4A}.Release|Any CPU.Build.0 = Release|Any CPU + {70B787F2-8B79-4AA5-8C73-26682A605B6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {70B787F2-8B79-4AA5-8C73-26682A605B6B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {70B787F2-8B79-4AA5-8C73-26682A605B6B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {70B787F2-8B79-4AA5-8C73-26682A605B6B}.Release|Any CPU.Build.0 = Release|Any CPU + {28692D43-3E08-43E4-BCBB-5940B9F741C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {28692D43-3E08-43E4-BCBB-5940B9F741C6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {28692D43-3E08-43E4-BCBB-5940B9F741C6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {28692D43-3E08-43E4-BCBB-5940B9F741C6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -55,6 +67,8 @@ Global {D91C182D-DC97-4F9B-AFFE-8C7A62501DA6} = {660FEA1B-C886-4B72-AD70-1D7DD248CD76} {4C5B170E-CAA1-421E-9FA5-9C2D6BEDB27C} = {460F2EC0-8774-4A26-835C-2FFFCAEEE1FA} {7123A8AE-678D-4D14-82E5-EB5607ABDC4A} = {460F2EC0-8774-4A26-835C-2FFFCAEEE1FA} + {70B787F2-8B79-4AA5-8C73-26682A605B6B} = {660FEA1B-C886-4B72-AD70-1D7DD248CD76} + {28692D43-3E08-43E4-BCBB-5940B9F741C6} = {660FEA1B-C886-4B72-AD70-1D7DD248CD76} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4388262D-A716-4918-A629-6072E4F8B668} diff --git a/src/NetArchTest.Rules/Extensions/TypeDefinitionExtensions.cs b/src/NetArchTest.Rules/Extensions/TypeDefinitionExtensions.cs index a4e786c..93cb15d 100644 --- a/src/NetArchTest.Rules/Extensions/TypeDefinitionExtensions.cs +++ b/src/NetArchTest.Rules/Extensions/TypeDefinitionExtensions.cs @@ -20,14 +20,34 @@ public static bool IsSubclassOf(this TypeDefinition child, TypeDefinition parent { if (parent != null) { - return child.MetadataToken - != parent.MetadataToken - && child.EnumerateBaseClasses().Any(b => b.MetadataToken == parent.MetadataToken); - } - else - { - return false; + return !child.IsSameTypeAs(parent) + && child.EnumerateBaseClasses().Any(b => b.IsSameTypeAs(parent)); } + + return false; + } + + /// + /// Tests whether two type definitions are from the same assembly. + /// The comparison is based on the full assembly names. + /// + /// + /// + /// An indication of whether the both types are from the same assembly. + public static bool IsFromSameAssemblyAs(this TypeDefinition a, TypeDefinition b) + { + return a.Module.Assembly.ToString() == b.Module.Assembly.ToString(); + } + + /// + /// Tests whether the provided types are the same type. + /// + /// + /// + /// An indication of whether the types are the same. + public static bool IsSameTypeAs(this TypeDefinition a, TypeDefinition b) + { + return a.IsFromSameAssemblyAs(b) && a.MetadataToken == b.MetadataToken; } /// diff --git a/src/NetArchTest.Rules/NetArchTest.Rules.xml b/src/NetArchTest.Rules/NetArchTest.Rules.xml index 5e69162..e23dab1 100644 --- a/src/NetArchTest.Rules/NetArchTest.Rules.xml +++ b/src/NetArchTest.Rules/NetArchTest.Rules.xml @@ -683,6 +683,23 @@ The parent that is inherited. An indication of whether the child inherits from the parent. + + + Tests whether two type definitions are from the same assembly. + The comparison is based on the full assembly names. + + + + An indication of whether the both types are from the same assembly. + + + + Tests whether the provided types are the same type. + + + + An indication of whether the types are the same. + Enumerate the base classes throughout the chain of inheritence. diff --git a/test/NetArchTest.CrossAssemblyTest.A/BaseClassFromA.cs b/test/NetArchTest.CrossAssemblyTest.A/BaseClassFromA.cs new file mode 100644 index 0000000..0910c6e --- /dev/null +++ b/test/NetArchTest.CrossAssemblyTest.A/BaseClassFromA.cs @@ -0,0 +1,6 @@ +namespace NetArchTest.CrossAssemblyTest.A +{ + public class BaseClassFromA + { + } +} \ No newline at end of file diff --git a/test/NetArchTest.CrossAssemblyTest.A/NetArchTest.CrossAssemblyTest.A.csproj b/test/NetArchTest.CrossAssemblyTest.A/NetArchTest.CrossAssemblyTest.A.csproj new file mode 100644 index 0000000..2756020 --- /dev/null +++ b/test/NetArchTest.CrossAssemblyTest.A/NetArchTest.CrossAssemblyTest.A.csproj @@ -0,0 +1,7 @@ + + + + netstandard2.0 + + + diff --git a/test/NetArchTest.CrossAssemblyTest.B/AnotherDerivedClassFromB.cs b/test/NetArchTest.CrossAssemblyTest.B/AnotherDerivedClassFromB.cs new file mode 100644 index 0000000..656022b --- /dev/null +++ b/test/NetArchTest.CrossAssemblyTest.B/AnotherDerivedClassFromB.cs @@ -0,0 +1,8 @@ +using NetArchTest.CrossAssemblyTest.A; + +namespace NetArchTest.CrossAssemblyTest.B +{ + public class AnotherDerivedClassFromB : BaseClassFromA + { + } +} \ No newline at end of file diff --git a/test/NetArchTest.CrossAssemblyTest.B/DerivedClassFromB.cs b/test/NetArchTest.CrossAssemblyTest.B/DerivedClassFromB.cs new file mode 100644 index 0000000..f27b644 --- /dev/null +++ b/test/NetArchTest.CrossAssemblyTest.B/DerivedClassFromB.cs @@ -0,0 +1,8 @@ +using NetArchTest.CrossAssemblyTest.A; + +namespace NetArchTest.CrossAssemblyTest.B +{ + public class DerivedClassFromB : BaseClassFromA + { + } +} \ No newline at end of file diff --git a/test/NetArchTest.CrossAssemblyTest.B/NetArchTest.CrossAssemblyTest.B.csproj b/test/NetArchTest.CrossAssemblyTest.B/NetArchTest.CrossAssemblyTest.B.csproj new file mode 100644 index 0000000..ece86bc --- /dev/null +++ b/test/NetArchTest.CrossAssemblyTest.B/NetArchTest.CrossAssemblyTest.B.csproj @@ -0,0 +1,11 @@ + + + + netstandard2.0 + + + + + + + diff --git a/test/NetArchTest.Rules.UnitTests/NetArchTest.Rules.UnitTests.csproj b/test/NetArchTest.Rules.UnitTests/NetArchTest.Rules.UnitTests.csproj index f9c6e49..11d86a4 100644 --- a/test/NetArchTest.Rules.UnitTests/NetArchTest.Rules.UnitTests.csproj +++ b/test/NetArchTest.Rules.UnitTests/NetArchTest.Rules.UnitTests.csproj @@ -17,6 +17,7 @@ + diff --git a/test/NetArchTest.Rules.UnitTests/PredicateTests.cs b/test/NetArchTest.Rules.UnitTests/PredicateTests.cs index 57e42fd..32c925c 100644 --- a/test/NetArchTest.Rules.UnitTests/PredicateTests.cs +++ b/test/NetArchTest.Rules.UnitTests/PredicateTests.cs @@ -3,6 +3,8 @@ using System; using System.Linq; using System.Reflection; + using NetArchTest.CrossAssemblyTest.A; + using NetArchTest.CrossAssemblyTest.B; using NetArchTest.TestStructure.Abstract; using NetArchTest.TestStructure.Classes; using NetArchTest.TestStructure.CustomAttributes; @@ -194,6 +196,20 @@ public void Inherit_MatchesFound_ClassesSelected() Assert.Contains(typeof(DerivedDerivedClass), result); } + [Fact(DisplayName = "Types can be selected if they inherit from a type from a different assembly")] + public void Inherit_MatchesFound_ClassesSelected_AcrossAssemblies() + { + var result = Types + .InAssembly(Assembly.GetAssembly(typeof(DerivedClassFromB))) + .That() + .Inherit(typeof(BaseClassFromA)) + .GetTypes(); + + Assert.Equal(2, result.Count()); + Assert.Contains(typeof(DerivedClassFromB), result); + Assert.Contains(typeof(AnotherDerivedClassFromB), result); + } + [Fact(DisplayName = "Types can be selected if they do not inherit from a type.")] public void DoNotInherit_MatchesFound_ClassesSelected() {