|
using System;
using System.Reflection;
using System.Diagnostics;
using System.Xml.Serialization;
namespace XsdTidy
{
/// <summary>
/// Helper static class for Type related tasks
/// </summary>
/// <include file="GUnit.Core.Doc.xml" path="doc/remarkss/remarks[@name='TypeHelper']"/>
public sealed class TypeHelper
{
internal TypeHelper()
{}
public static ConstructorInfo GetConstructor(Type t, params Type[] args)
{
if (t==null)
throw new ArgumentNullException("t");
ConstructorInfo ci = t.GetConstructor(args);
if (ci==null)
throw new ArgumentNullException("constructor for " + t.FullName +" not found");
return ci;
}
public static ConstructorInfo GetDefaultConstructor(Type t)
{
if (t==null)
throw new ArgumentNullException("t");
ConstructorInfo ci = t.GetConstructor(Type.EmptyTypes);
if (ci==null)
throw new ArgumentNullException("no default constructor for " + t.FullName );
return ci;
}
public static bool IsXmlNullable(FieldInfo f)
{
if (f.FieldType.IsValueType)
return false;
if (!HasCustomAttribute(f,typeof(XmlElementAttribute)))
return true;
XmlElementAttribute attr =
(XmlElementAttribute)GetFirstCustomAttribute(f,typeof(XmlElementAttribute));
return attr.IsNullable;
}
/// <summary>
/// Gets a value indicating if the type <paramref name="t"/> is tagged
/// by a <paramref name="customAttributeType"/> instance.
/// </summary>
/// <param name="t">type to test</param>
/// <param name="customAttributeType">custom attribute type to search</param>
/// <returns>
/// true if <param name="t"/> is tagged by a <paramref name="customAttributeType"/>
/// attribute, false otherwise.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="t"/> or <paramref name="customAttributeType"/>
/// is a null reference
/// </exception>
/// <remarks>
/// You can use this method to check that a type is tagged by an attribute.
/// </remarks>
public static bool HasCustomAttribute(Type t,Type customAttributeType)
{
if (t==null)
throw new ArgumentNullException("t");
if (customAttributeType==null)
throw new ArgumentNullException("customAttributeType");
return t.GetCustomAttributes(customAttributeType,true).Length != 0;
}
/// <summary>
/// Gets a value indicating if the property info <paramref name="t"/> is tagged
/// by a <paramref name="customAttributeType"/> instance.
/// </summary>
/// <param name="t">property to test</param>
/// <param name="customAttributeType">custom attribute type to search</param>
/// <returns>
/// true if <param name="t"/> is tagged by a <paramref name="customAttributeType"/>
/// attribute, false otherwise.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="t"/> or <paramref name="customAttributeType"/>
/// is a null reference
/// </exception>
/// <remarks>
/// You can use this property to check that a method is tagged by a
/// specified attribute.
/// </remarks>
public static bool HasCustomAttribute(PropertyInfo t,Type customAttributeType)
{
if (t==null)
throw new ArgumentNullException("t");
if (customAttributeType==null)
throw new ArgumentNullException("customAttributeType");
return t.GetCustomAttributes(customAttributeType,true).Length != 0;
}
public static bool HasCustomAttribute(FieldInfo t,Type customAttributeType)
{
if (t==null)
throw new ArgumentNullException("t");
if (customAttributeType==null)
throw new ArgumentNullException("customAttributeType");
return t.GetCustomAttributes(customAttributeType,true).Length != 0;
}
/// <summary>
/// Gets the first instance of <paramref name="customAttributeType"/>
/// from the type <paramref name="t"/> custom attributes.
/// </summary>
/// <param name="t">type to test</param>
/// <param name="customAttributeType">custom attribute type to search</param>
/// <returns>
/// First instance of <paramref name="customAttributeTyp"/>
/// from the type <paramref name="t"/> custom attributes.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="t"/> or <paramref name="customAttributeType"/>
/// is a null reference
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="t"/> is not tagged by an attribute of type
/// <paramref name="customAttributeType"/>
/// </exception>
/// <remarks>
/// You can use this method to retreive a specified attribute
/// instance
/// </remarks>
public static Object GetFirstCustomAttribute(Type t, Type customAttributeType)
{
if (t==null)
throw new ArgumentNullException("t");
if (customAttributeType==null)
throw new ArgumentNullException("customAttributeType");
Object[] attrs = t.GetCustomAttributes(customAttributeType,true);
if (attrs.Length==0)
throw new ArgumentException("type does not have custom attribute");
return attrs[0];
}
/// <summary>
/// Gets the first instance of <paramref name="customAttributeType"/>
/// from the property <paramref name="mi"/> custom attributes.
/// </summary>
/// <param name="mi">property to test</param>
/// <param name="customAttributeType">custom attribute type to search</param>
/// <returns>
/// First instance of <paramref name="customAttributeTyp"/>
/// from the property <paramref name="mi"/> custom attributes.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="mi"/> or <paramref name="customAttributeType"/>
/// is a null reference
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="mi"/> is not tagged by an attribute of type
/// <paramref name="customAttributeType"/>
/// </exception>
/// <remarks>
/// You can use this property to retreive a specified attribute
/// instance of a method.
/// </remarks>
public static Object GetFirstCustomAttribute(PropertyInfo mi, Type customAttributeType)
{
if (mi==null)
throw new ArgumentNullException("mi");
if (customAttributeType==null)
throw new ArgumentNullException("customAttributeType");
Object[] attrs = mi.GetCustomAttributes(customAttributeType,true);
if (attrs.Length==0)
throw new ArgumentException("type does not have custom attribute");
return attrs[0];
}
public static Object GetFirstCustomAttribute(FieldInfo mi, Type customAttributeType)
{
if (mi==null)
throw new ArgumentNullException("mi");
if (customAttributeType==null)
throw new ArgumentNullException("customAttributeType");
Object[] attrs = mi.GetCustomAttributes(customAttributeType,true);
if (attrs.Length==0)
throw new ArgumentException("type does not have custom attribute");
return attrs[0];
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).