Skip to content

Commit 90ea520

Browse files
committed
remove Types.InNamespace
1 parent a0522e1 commit 90ea520

2 files changed

Lines changed: 16 additions & 81 deletions

File tree

src/NetArchTest.Rules/Types.cs

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
namespace NetArchTest.Rules
2-
{
3-
using System;
4-
using System.Collections.Generic;
5-
using System.IO;
6-
using System.Linq;
7-
using System.Reflection;
8-
using System.Runtime.CompilerServices;
9-
using NetArchTest.Rules.Extensions;
10-
using Mono.Cecil;
11-
using NetArchTest.Rules.Dependencies;
12-
using NetArchTest.Rules.Dependencies.DataStructures;
13-
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Runtime.CompilerServices;
7+
using NetArchTest.Rules.Extensions;
8+
using Mono.Cecil;
9+
using NetArchTest.Rules.Dependencies;
10+
using NetArchTest.Rules.Dependencies.DataStructures;
11+
12+
namespace NetArchTest.Rules
13+
{
1414
/// <summary>
1515
/// Creates a list of types that can have predicates and conditions applied to it.
1616
/// </summary>
@@ -112,48 +112,7 @@ public static Types InAssemblies(IEnumerable<Assembly> assemblies, IEnumerable<s
112112
return new Types(types);
113113
}
114114

115-
/// <summary>
116-
/// Creates a list of all the types in a particular namespace.
117-
/// </summary>
118-
/// <param name="name">The namespace to list types for. This is case insensitive.</param>
119-
/// <returns>A list of types that can have predicates and conditions applied to it.</returns>
120-
public static Types InNamespace(string name)
121-
{
122-
if (string.IsNullOrEmpty(name))
123-
{
124-
throw new ArgumentNullException(nameof(name));
125-
}
126-
127-
// We need to check all the assemblies in the domain
128-
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
129-
var types = new List<TypeDefinition>();
130-
131-
foreach (var assembly in assemblies)
132-
{
133-
if (!assembly.IsDynamic)
134-
{
135-
// Load the assembly using Mono.Cecil.
136-
var assemblyDef = ReadAssemblyDefinition(assembly.Location);
137-
138-
if (assemblyDef != null)
139-
{
140-
// Read all the types in the assembly
141-
var matches = (assemblyDef.Modules
142-
.SelectMany(t => t.Types)
143-
.Where(t => t.Namespace != null && t.Namespace.StartsWith(name, StringComparison.InvariantCultureIgnoreCase)))
144-
.ToList();
145-
146-
if (matches.Count > 0)
147-
{
148-
types.AddRange(matches);
149-
}
150-
}
151-
}
152-
}
153-
154-
var list = Types.GetAllTypes(types);
155-
return new Types(list);
156-
}
115+
157116

158117
/// <summary>
159118
/// Creates a list of all the types in a particular module file.

test/NetArchTest.Rules.UnitTests/TypesTests.cs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,7 @@ public void InCurrentDomain_NestedPrivateTypesPresent_Returned()
7070
Assert.Contains(result, t => t.FullName.StartsWith("NetArchTest.TestStructure.Nested.NestedPrivate/NestedPrivateClass"));
7171
}
7272

73-
[Fact(DisplayName = "A types collection can be created from a namespace.")]
74-
public void InNamespace_TypesReturned()
75-
{
76-
var result = Types.InNamespace("NetArchTest.TestStructure.NameMatching").GetTypes();
77-
78-
Assert.Equal(9, result.Count()); // Nine types found
79-
Assert.Contains<Type>(typeof(ClassA1), result);
80-
Assert.Contains<Type>(typeof(ClassA2), result);
81-
Assert.Contains<Type>(typeof(ClassA3), result);
82-
Assert.Contains<Type>(typeof(ClassB1), result);
83-
Assert.Contains<Type>(typeof(ClassB2), result);
84-
Assert.Contains<Type>(typeof(SomeThing), result);
85-
Assert.Contains<Type>(typeof(SomethingElse), result);
86-
Assert.Contains<Type>(typeof(SomeEntity), result);
87-
Assert.Contains<Type>(typeof(SomeIdentity), result);
88-
}
73+
8974

9075
[Fact(DisplayName = "A types collection can be created from a filename.")]
9176
public void FromFile_TypesReturned()
@@ -125,15 +110,6 @@ public void FromFile_BadImage_CaughtAndEmptyListReturned()
125110
Assert.Empty(result);
126111
}
127112

128-
[Fact(DisplayName = "Any compiler generated classes will be ignored in a types list.")]
129-
public void InNamespace_CompilerGeneratedClasses_NotReturned()
130-
{
131-
// Act
132-
var result = Types.InNamespace("NetArchTest.TestStructure.Dependencies.Search").GetTypes();
133-
134-
// Assert
135-
var generated = result.Any(r => r.CustomAttributes.Any(x => x?.AttributeType?.FullName == typeof(CompilerGeneratedAttribute).FullName));
136-
Assert.False(generated);
137-
}
113+
138114
}
139115
}

0 commit comments

Comments
 (0)