Пример #1
0
		/// <summary>
		///   Compiles the <paramref name="syntaxTree" />, instantiates all non-abstract classes implementing
		///   <see cref="ITestableObject" />, and returns them.
		/// </summary>
		/// <param name="syntaxTree">The syntax tree that should be compiled.</param>
		protected IEnumerable<ITestableObject> GetTestableObjects(SyntaxTree syntaxTree)
		{
			var compilation = CreateCompilation(syntaxTree);
			var semanticModel = compilation.GetSemanticModel(syntaxTree);

			var testableTypes = syntaxTree
				.Descendants<ClassDeclarationSyntax>()
				.Select(declaration => declaration.GetTypeSymbol(semanticModel))
				.Where(symbol => !symbol.IsGenericType && !symbol.IsAbstract && symbol.ContainingType == null)
				.Where(symbol => symbol.IsDerivedFrom(semanticModel.GetTypeSymbol<ITestableObject>()))
				.Select(symbol => symbol.ToDisplayString())
				.ToArray();

			if (testableTypes.Length == 0)
				throw new TestException("Unable to find any testable class declarations.");

			var assembly = CompileSafetySharp(compilation);
			return testableTypes.Select(testableType => (ITestableObject)Activator.CreateInstance(assembly.GetType(testableType)));
		}