Code:
/// <summary>
/// 0: API, 1: UI, -1: Not test dll
/// </summary>
/// <param name="dllFile"></param>
/// <returns></returns>
public static int IsTestDll(string dllFile)
{
try
{
List<MethodInfo> methodList = new List<MethodInfo>();
Assembly assembly = Assembly.LoadFrom(dllFile);
for (int i = 0; i < assembly.GetTypes().Count(); i++)
{
IList<CustomAttributeData> customAttributes = assembly.GetTypes()[i].GetCustomAttributesData();
foreach (var attr in customAttributes) //assembly.GetTypes()[i].CustomAttributes)
{
string attrType = attr.AttributeType.ToString();
if (attrType.Equals("Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute"))
{
return 0;
}
else if (attrType.Equals("Microsoft.VisualStudio.TestTools.UITesting.CodedUITestAttribute"))
{
return 1;
}
}
}
return -1;
}
catch
{
return -1;
}
}
最近总被dll所恼, 说明还不了解其原理, 慢慢学习。