-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParsingAssembly.cs
55 lines (50 loc) · 1.7 KB
/
ParsingAssembly.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Mono.Cecil;
namespace TfsSyncUtility
{
public class ParsingAssembly
{
//*****Variables********
readonly string unitTestAttribute = ConfigurationManager.AppSettings["unitTestAttribute"];
readonly string tcAttribute = ConfigurationManager.AppSettings["customTcAttribute"];
private readonly string _testAssembly;
public ParsingAssembly(string testAssembly)
{
this._testAssembly = testAssembly;
}
public Dictionary<Int64, string> GetTestMethods()
{
var idNamePair = new Dictionary<Int64, string>();
var assembly = AssemblyDefinition.ReadAssembly(_testAssembly);
var allMethods = assembly.MainModule.Types
.SelectMany(t => t.Methods);
foreach (var methodDefinition in allMethods)
{
var hasUnitTestAttribute = methodDefinition.CustomAttributes.Any(a => a.AttributeType.FullName == unitTestAttribute);
var hasTcAttribute = methodDefinition.CustomAttributes.Any(a => a.AttributeType.FullName == tcAttribute);
if (hasUnitTestAttribute && hasTcAttribute)
{
var tcId = 0;
string menthodName = methodDefinition.DeclaringType.FullName + "." + methodDefinition.Name;
CustomAttribute firstOrDefault = methodDefinition.CustomAttributes.FirstOrDefault(a => a.AttributeType.FullName == tcAttribute);
if (firstOrDefault != null)
tcId = Convert.ToInt32(firstOrDefault.ConstructorArguments[0].Value);
try
{
if (!idNamePair.ContainsKey(tcId))
idNamePair.Add(tcId, menthodName);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}
}
return idNamePair;
}
}
}