1 // ***********************************************************************
2 // Copyright (c) 2015 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
31 using System.Reflection;
32 using NUnit.Compatibility;
33 using NUnit.Framework.Interfaces;
34 using System.Collections.Generic;
36 using NUnit.Framework.TUnit;
38 namespace NUnit.Framework.Internal
41 /// The TypeWrapper class wraps a Type so it may be used in
42 /// a platform-independent manner.
44 public class TypeWrapper : ITypeInfo
47 /// Construct a TypeWrapper for a specified Type.
49 public TypeWrapper(Type type)
51 Guard.ArgumentNotNull(type, "Type");
57 /// Gets the underlying Type on which this TypeWrapper is based.
59 public Type Type { get; private set; }
62 /// Gets the base type of this type as an ITypeInfo
64 public ITypeInfo BaseType
68 var baseType = Type.GetTypeInfo().BaseType;
70 return baseType != null
71 ? new TypeWrapper(baseType)
77 /// Gets the Name of the Type
81 get { return Type.Name; }
85 /// Gets the FullName of the Type
87 public string FullName
89 get { return Type.FullName; }
93 /// Gets the assembly in which the type is declared
95 public Assembly Assembly
97 get { return Type.GetTypeInfo().Assembly; }
101 /// Gets the namespace of the Type
103 public string Namespace
105 get { return Type.Namespace; }
109 /// Gets a value indicating whether the type is abstract.
111 public bool IsAbstract
113 get { return Type.GetTypeInfo().IsAbstract; }
117 /// Gets a value indicating whether the Type is a generic Type
119 public bool IsGenericType
121 get { return Type.GetTypeInfo().IsGenericType; }
125 /// Returns true if the Type wrapped is T
127 public bool IsType(Type type)
133 /// Gets a value indicating whether the Type has generic parameters that have not been replaced by specific Types.
135 public bool ContainsGenericParameters
137 get { return Type.GetTypeInfo().ContainsGenericParameters; }
141 /// Gets a value indicating whether the Type is a generic Type definition
143 public bool IsGenericTypeDefinition
145 get { return Type.GetTypeInfo().IsGenericTypeDefinition; }
149 /// Gets a value indicating whether the type is sealed.
153 get { return Type.GetTypeInfo().IsSealed; }
157 /// Gets a value indicating whether this type represents a static class.
159 public bool IsStaticClass
161 get { return Type.GetTypeInfo().IsSealed && Type.GetTypeInfo().IsAbstract; }
165 /// Get the display name for this type
167 public string GetDisplayName()
169 return TypeHelper.GetDisplayName(Type);
173 /// Get the display name for an object of this type, constructed with the specified args.
175 public string GetDisplayName(object[] args)
177 return TypeHelper.GetDisplayName(Type, args);
181 /// Returns a new ITypeInfo representing an instance of this generic Type using the supplied Type arguments
183 public ITypeInfo MakeGenericType(Type[] typeArgs)
185 return new TypeWrapper(Type.MakeGenericType(typeArgs));
189 /// Returns a Type representing a generic type definition from which this Type can be constructed.
191 public Type GetGenericTypeDefinition()
193 return Type.GetGenericTypeDefinition();
197 /// Returns an array of custom attributes of the specified type applied to this type
199 public T[] GetCustomAttributes<T>(bool inherit) where T : class
202 var allAttribute = Type.GetTypeInfo().GetCustomAttributes();
203 List<string> attributeDic = new List<string>();
204 foreach (Attribute atb in allAttribute)
206 attributeDic.Add(atb.GetType().FullName);
209 List<T> objects = new List<T>();
211 string path = System.IO.Path.GetDirectoryName(Assembly.Location);
212 if (!Directory.Exists(path))
214 TLogger.WriteError(TLogger.ExceptionTag, "" + path + " - not a directory");
215 return objects.ToArray();
217 foreach (var assemblyPath in Directory.GetFiles(path, "*.Tests.dll"))
219 IEnumerable<Type> types;
222 Assembly please = AssemblyHelper.Load(assemblyPath);
223 if (please == null) continue;
224 types = please.GetTypes().Where(p => !p.GetTypeInfo().IsAbstract && p.GetTypeInfo().IsClass && p.GetTypeInfo().ImplementedInterfaces.Contains(typeof(T)));
230 for (int i = 0; i < types.Count(); i++)
234 if (attributeDic.Contains(types.ElementAt(i).FullName))
236 objects.Add((T)Activator.CreateInstance(types.ElementAt(i)));
245 return objects.ToArray();
247 return (T[])Type.GetCustomAttributes(typeof(T), inherit);
252 /// Returns a value indicating whether the type has an attribute of the specified type.
254 /// <typeparam name="T"></typeparam>
255 /// <param name="inherit"></param>
256 /// <returns></returns>
257 public bool IsDefined<T>(bool inherit)
260 return Type.GetTypeInfo().GetCustomAttributes(inherit).Any(a => typeof(T).IsAssignableFrom(a.GetType()));
262 return Type.GetTypeInfo().IsDefined(typeof(T), inherit);
267 /// Returns a flag indicating whether this type has a method with an attribute of the specified type.
269 /// <param name="attributeType"></param>
270 /// <returns></returns>
271 public bool HasMethodWithAttribute(Type attributeType)
273 return Reflect.HasMethodWithAttribute(Type, attributeType);
277 /// Returns an array of IMethodInfos for methods of this Type
278 /// that match the specified flags.
280 public IMethodInfo[] GetMethods(BindingFlags flags)
282 var methods = Type.GetMethods(flags);
283 var result = new MethodWrapper[methods.Length];
285 for (int i = 0; i < methods.Length; i++)
286 result[i] = new MethodWrapper(Type, methods[i]);
292 /// Gets the public constructor taking the specified argument Types
294 public ConstructorInfo GetConstructor(Type[] argTypes)
296 return Type.GetConstructors()
297 .Where(c => c.GetParameters().ParametersMatch(argTypes))
302 /// Returns a value indicating whether this Type has a public constructor taking the specified argument Types.
304 public bool HasConstructor(Type[] argTypes)
306 return GetConstructor(argTypes) != null;
310 /// Construct an object of this Type, using the specified arguments.
312 public object Construct(object[] args)
314 return Reflect.Construct(Type, args);
318 /// Override ToString() so that error messages in NUnit's own tests make sense
320 public override string ToString()
322 return Type.ToString();
326 private string GetAssemblyName(string assemblyFullPath)
329 string[] delimiter1 = { "\\" };
330 string[] delimiter2 = { "/" };
331 string[] delimiterDot = { "." };
333 string returnValue = "";
336 strAry = assemblyFullPath.Split(delimiter1, StringSplitOptions.None);
338 if (strAry.Length < 2)
339 strAry = assemblyFullPath.Split(delimiter2, StringSplitOptions.None);
341 foreach (string str in strAry)
343 if (str.Contains("Tests.dll"))
345 string[] strSplit = str.Split(delimiterDot, StringSplitOptions.None);
346 returnValue = strSplit[0];
347 // LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, "check : "+ returnValue);
354 LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, e.ToString());