[gitattributes] Do CRLF normalization on sln/proj files
[mono-project.git] / mcs / nunit24 / NUnitCore / core / Reflect.cs
blob38a49ebd4014752adc9c692e788da508123b0b8b
1 // ****************************************************************
2 // This is free software licensed under the NUnit license. You
3 // may obtain a copy of the license as well as information regarding
4 // copyright ownership at http://nunit.org/?p=license&r=2.4.
5 // ****************************************************************
7 using System;
8 using System.Reflection;
9 using System.Collections;
11 namespace NUnit.Core
13 /// <summary>
14 /// Helper methods for inspecting a type by reflection.
15 ///
16 /// Many of these methods take a MemberInfo as an argument to avoid
17 /// duplication, even though certain attributes can only appear on
18 /// specific types of members, like MethodInfo or Type.
19 ///
20 /// In the case where a type is being examined for the presence of
21 /// an attribute, interface or named member, the Reflect methods
22 /// operate with the full name of the member being sought. This
23 /// removes the necessity of the caller having a reference to the
24 /// assembly that defines the item being sought and allows the
25 /// NUnit core to inspect assemblies that reference an older
26 /// version of the NUnit framework.
27 /// </summary>
28 public class Reflect
30 #region Attributes
32 /// <summary>
33 /// Check presence of attribute of a given type on a member.
34 /// </summary>
35 /// <param name="member">The member to examine</param>
36 /// <param name="attrName">The FullName of the attribute type to look for</param>
37 /// <param name="inherit">True to include inherited attributes</param>
38 /// <returns>True if the attribute is present</returns>
39 public static bool HasAttribute( MemberInfo member, string attrName, bool inherit )
41 object[] attributes = member.GetCustomAttributes( inherit );
42 foreach( Attribute attribute in attributes )
43 if ( IsInstanceOfType( attrName, attribute ) )
44 return true;
45 return false;
48 /// <summary>
49 /// Get attribute of a given type on a member. If multiple attributes
50 /// of a type are present, the first one found is returned.
51 /// </summary>
52 /// <param name="member">The member to examine</param>
53 /// <param name="attrName">The FullName of the attribute type to look for</param>
54 /// <param name="inherit">True to include inherited attributes</param>
55 /// <returns>The attribute or null</returns>
56 public static System.Attribute GetAttribute(MemberInfo member, string attrName, bool inherit)
58 object[] attributes = member.GetCustomAttributes(inherit);
59 foreach (Attribute attribute in attributes)
60 if ( IsInstanceOfType( attrName, attribute ) )
61 return attribute;
62 return null;
65 /// <summary>
66 /// Get attribute of a given type on an assembly. If multiple attributes
67 /// of a type are present, the first one found is returned.
68 /// </summary>
69 /// <param name="assembly">The assembly to examine</param>
70 /// <param name="attrName">The FullName of the attribute type to look for</param>
71 /// <param name="inherit">True to include inherited attributes</param>
72 /// <returns>The attribute or null</returns>
73 public static System.Attribute GetAttribute(Assembly assembly, string attrName, bool inherit)
75 object[] attributes = assembly.GetCustomAttributes(inherit);
76 foreach (Attribute attribute in attributes)
77 if ( IsInstanceOfType(attrName, attribute) )
78 return attribute;
79 return null;
82 /// <summary>
83 /// Get all attributes of a given type on a member.
84 /// </summary>
85 /// <param name="member">The member to examine</param>
86 /// <param name="attrName">The FullName of the attribute type to look for</param>
87 /// <param name="inherit">True to include inherited attributes</param>
88 /// <returns>The attribute or null</returns>
89 public static System.Attribute[] GetAttributes( MemberInfo member, string attrName, bool inherit )
91 object[] attributes = member.GetCustomAttributes( inherit );
92 ArrayList result = new ArrayList();
93 foreach( Attribute attribute in attributes )
94 if ( IsInstanceOfType( attrName, attribute ) )
95 result.Add( attribute );
96 return (System.Attribute[])result.ToArray( typeof( System.Attribute ) );
99 /// <summary>
100 /// Get all attributes on a member.
101 /// </summary>
102 /// <param name="member">The member to examine</param>
103 /// <param name="inherit">True to include inherited attributes</param>
104 /// <returns>The attribute or null</returns>
105 public static System.Attribute[] GetAttributes(MemberInfo member, bool inherit)
107 object[] attributes = member.GetCustomAttributes(inherit);
108 System.Attribute[] result = new System.Attribute[attributes.Length];
109 int n = 0;
110 foreach (Attribute attribute in attributes)
111 result[n++] = attribute;
112 return result;
115 /// <summary>
116 /// Get all attributes on an assembly.
117 /// </summary>
118 /// <param name="assembly">The assembly to examine</param>
119 /// <param name="inherit">True to include inherited attributes</param>
120 /// <returns>The attributes or null</returns>
121 public static System.Attribute[] GetAttributes(Assembly assembly, bool inherit)
123 object[] attributes = assembly.GetCustomAttributes(inherit);
124 System.Attribute[] result = new System.Attribute[attributes.Length];
125 int n = 0;
126 foreach (Attribute attribute in attributes)
127 result[n++] = attribute;
128 return result;
131 #endregion
133 #region Interfaces
135 /// <summary>
136 /// Check to see if a type implements a named interface.
137 /// </summary>
138 /// <param name="fixtureType">The type to examine</param>
139 /// <param name="interfaceName">The FullName of the interface to check for</param>
140 /// <returns>True if the interface is implemented by the type</returns>
141 public static bool HasInterface( Type fixtureType, string interfaceName )
143 foreach( Type type in fixtureType.GetInterfaces() )
144 if ( type.FullName == interfaceName )
145 return true;
146 return false;
149 #endregion
151 #region Inheritance
152 //SHMARYA: [ 10/12/2005 ]
153 /// <summary>
154 /// Checks to see if a type inherits from a named type.
155 /// </summary>
156 /// <param name="type">The type to examine</param>
157 /// <param name="parentType">The FullName of the inherited type to look for</param>
158 /// <returns>True if the type inherits from the named type.</returns>
159 public static bool InheritsFrom( Type type, string typeName )
161 for( Type current = type; current != typeof( object ); current = current.BaseType )
162 if( current.FullName == typeName )
163 return true;
165 return false;
168 public static bool InheritsFrom( object obj, string typeName )
170 return InheritsFrom( obj.GetType(), typeName );
173 public static bool IsInstanceOfType( string typeName, Attribute attr )
175 Type type = attr.GetType();
176 return type.FullName == typeName || InheritsFrom( type, typeName );
178 #endregion
180 #region Get Methods of a type
182 /// <summary>
183 /// Find the default constructor on a type
184 /// </summary>
185 /// <param name="fixtureType"></param>
186 /// <returns></returns>
187 public static ConstructorInfo GetConstructor( Type fixtureType )
189 return fixtureType.GetConstructor( Type.EmptyTypes );
192 /// <summary>
193 /// Find the default constructor on a type
194 /// </summary>
195 /// <param name="fixtureType"></param>
196 /// <returns></returns>
197 public static ConstructorInfo GetConstructor( Type fixtureType, Type[] types )
199 return fixtureType.GetConstructor( types );
202 /// <summary>
203 /// Examine a fixture type and return a method having a particular attribute.
204 /// In the case of multiple methods, the first one found is returned.
205 /// </summary>
206 /// <param name="fixtureType">The type to examine</param>
207 /// <param name="attributeName">The FullName of the attribute to look for</param>
208 /// <param name="bindingFlags">BindingFlags to use in looking for method</param>
209 /// <returns>A MethodInfo or null</returns>
210 public static MethodInfo GetMethodWithAttribute( Type fixtureType, string attributeName, BindingFlags bindingFlags, bool inherit )
212 foreach(MethodInfo method in fixtureType.GetMethods( bindingFlags ) )
214 if( HasAttribute( method, attributeName, inherit ) )
215 return method;
218 return null;
221 /// <summary>
222 /// Examine a fixture type and return a count of the methods having a
223 /// particular attribute.
224 /// </summary>
225 /// <param name="fixtureType">The type to examine</param>
226 /// <param name="attributeName">The FullName of the attribute to look for</param>
227 /// <param name="bindingFlags">BindingFlags to use in looking for method</param>
228 /// <returns>The number of such methods found</returns>
229 public static int CountMethodsWithAttribute( Type fixtureType, string attributeName, BindingFlags bindingFlags, bool inherit )
231 int count = 0;
233 foreach(MethodInfo method in fixtureType.GetMethods( bindingFlags ) )
235 if( HasAttribute( method, attributeName, inherit ) )
236 count++;
239 return count;
242 /// <summary>
243 /// Examine a fixture type and get a method with a particular name.
244 /// In the case of overloads, the first one found is returned.
245 /// </summary>
246 /// <param name="fixtureType">The type to examine</param>
247 /// <param name="methodName">The name of the method</param>
248 /// <param name="bindingFlags">BindingFlags to use in the search</param>
249 /// <returns>A MethodInfo or null</returns>
250 public static MethodInfo GetNamedMethod(Type fixtureType, string methodName, BindingFlags bindingFlags)
252 foreach (MethodInfo method in fixtureType.GetMethods(bindingFlags))
254 if (method.Name == methodName)
255 return method;
258 return null;
261 /// <summary>
262 /// Examine a fixture type and get a method with a particular name and list
263 /// of arguments. In the case of overloads, the first one found is returned.
264 /// </summary>
265 /// <param name="fixtureType">The type to examine</param>
266 /// <param name="methodName">The name of the method</param>
267 /// <param name="argTypes">The full names of the argument types to search for</param>
268 /// <param name="bindingFlags">BindingFlags to use in the search</param>
269 /// <returns>A MethodInfo or null</returns>
270 public static MethodInfo GetNamedMethod(Type fixtureType, string methodName,
271 string[] argTypes, BindingFlags bindingFlags)
273 foreach (MethodInfo method in fixtureType.GetMethods(bindingFlags))
275 if (method.Name == methodName)
277 ParameterInfo[] parameters = method.GetParameters();
278 if (parameters.Length == argTypes.Length)
280 bool match = true;
281 for (int i = 0; i < argTypes.Length; i++)
282 if (parameters[i].ParameterType.FullName != argTypes[i])
284 match = false;
285 break;
288 if (match)
289 return method;
294 return null;
297 #endregion
299 #region Get Properties of a type
301 /// <summary>
302 /// Examine a type and return a property having a particular attribute.
303 /// In the case of multiple methods, the first one found is returned.
304 /// </summary>
305 /// <param name="fixtureType">The type to examine</param>
306 /// <param name="attributeName">The FullName of the attribute to look for</param>
307 /// <param name="bindingFlags">Binding flags to use in searching</param>
308 /// <returns>A PropertyInfo or null</returns>
309 public static PropertyInfo GetPropertyWithAttribute( Type fixtureType, string attributeName, BindingFlags bindingFlags )
311 foreach(PropertyInfo property in fixtureType.GetProperties( bindingFlags ) )
313 if( HasAttribute( property, attributeName, true ) )
314 return property;
317 return null;
320 /// <summary>
321 /// Examine a type and get a property with a particular name.
322 /// In the case of overloads, the first one found is returned.
323 /// </summary>
324 /// <param name="type">The type to examine</param>
325 /// <param name="bindingFlags">BindingFlags to use</param>
326 /// <returns>A PropertyInfo or null</returns>
327 public static PropertyInfo GetNamedProperty( Type type, string name, BindingFlags bindingFlags )
329 return type.GetProperty( name, bindingFlags );
332 /// <summary>
333 /// Get the value of a named property on an object using binding flags of Public and Instance
334 /// </summary>
335 /// <param name="obj">The object for which the property value is needed</param>
336 /// <param name="name">The name of a non-indexed property of the object</param>
337 /// <returns></returns>
338 public static object GetPropertyValue( object obj, string name )
340 return GetPropertyValue( obj, name, BindingFlags.Public | BindingFlags.Instance );
343 /// <summary>
344 /// Get the value of a named property on an object
345 /// </summary>
346 /// <param name="obj">The object for which the property value is needed</param>
347 /// <param name="name">The name of a non-indexed property of the object</param>
348 /// <param name="bindingFlags">BindingFlags for use in determining which properties are needed</param>param>
349 /// <returns></returns>
350 public static object GetPropertyValue( object obj, string name, BindingFlags bindingFlags )
352 PropertyInfo property = GetNamedProperty( obj.GetType(), name, bindingFlags );
353 if ( property != null )
354 return property.GetValue( obj, null );
355 return null;
358 /// <summary>
359 /// Set the value of a named property on an object
360 /// </summary>
361 /// <param name="obj">The object for which the property value is to be set</param>
362 /// <param name="name">The name of a non-indexed property of the object</param>
363 /// <param name="val">The value to which the property is to be set</param>
364 /// <param name="bindingFlags">BindingFlags for use in determining which properties are needed</param>param>
365 public static void SetPropertyValue( object obj, string name, object val, BindingFlags bindingFlags )
367 PropertyInfo property = GetNamedProperty( obj.GetType(), name, bindingFlags );
368 if ( property != null )
369 property.SetValue( obj, val, null );
372 #endregion
374 #region Invoke Methods
376 /// <summary>
377 /// Invoke the default constructor on a type
378 /// </summary>
379 /// <param name="type">The type to be constructed</param>
380 /// <returns>An instance of the type</returns>
381 public static object Construct( Type type )
383 ConstructorInfo ctor = GetConstructor( type );
384 if ( ctor == null )
385 throw new InvalidTestFixtureException(type.FullName + " does not have a valid constructor");
387 return ctor.Invoke( Type.EmptyTypes );
390 /// <summary>
391 /// Invoke a parameterless method returning void on an object.
392 /// </summary>
393 /// <param name="method">A MethodInfo for the method to be invoked</param>
394 /// <param name="fixture">The object on which to invoke the method</param>
395 public static void InvokeMethod( MethodInfo method, object fixture )
397 InvokeMethod( method, fixture, null );
400 /// <summary>
401 /// Invoke a method returning void, converting any TargetInvocationException
402 /// to an NUnitException
403 /// </summary>
404 /// <param name="method">A MethodInfo for the method to be invoked</param>
405 /// <param name="fixture">The object on which to invoke the method</param>
406 public static void InvokeMethod( MethodInfo method, object fixture, params object[] args )
408 if(method != null)
412 method.Invoke( fixture, args );
414 catch(TargetInvocationException e)
416 Exception inner = e.InnerException;
417 throw new NUnitException("Rethrown",inner);
422 #endregion
424 #region Private Constructor for static-only class
426 private Reflect() { }
428 #endregion