- nperf_src.zip
- Releases
- Unpackaged
- nperf-1.1-Release
- nperf
- src
- nperf_demo.zip
- bin
- Release
- NPerf.Cons.exe
- NPerf.Core.dll
- NPerf.Framework.dll
- NPerf.Report.dll
- nperf-doc-1.1.chm
- scpl.dll
- System.Perf.dll
- nperf_bin.zip
- Release
- NPerf.Cons.exe
- NPerf.Core.dll
- NPerf.Framework.chm
- NPerf.Framework.dll
- NPerf.Report.dll
- scpl.dll
- system.perf.bat
- System.Perf.dll
|
using System;
using System.Reflection;
namespace NPerf.Core
{
using NPerf.Core.Collections;
/// <summary>
/// Summary description for TypeHelper.
/// </summary>
public sealed class TypeHelper
{
internal TypeHelper()
{}
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;
}
public static bool HasCustomAttribute(MethodInfo 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 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];
}
public static Object GetFirstCustomAttribute(MethodInfo 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 MethodInfo GetAttributedMethod(Type t, Type customAttributeType)
{
if (t==null)
throw new ArgumentNullException("t");
if (customAttributeType==null)
throw new ArgumentNullException("customAttributeType");
foreach(MethodInfo m in t.GetMethods())
{
if (HasCustomAttribute(m,customAttributeType))
return m;
}
return null;
}
public static AttributedMethodCollection GetAttributedMethods(Type t, Type customAttributeType)
{
if (t==null)
throw new ArgumentNullException("t");
if (customAttributeType==null)
throw new ArgumentNullException("customAttributeType");
return new AttributedMethodCollection(t,customAttributeType);
}
public static void CheckSignature(MethodInfo mi, Type returnType, params Type[] argumentTypes)
{
if (mi==null)
throw new ArgumentNullException("mi");
if (returnType==null)
throw new ArgumentNullException("returnType");
if (mi.ReturnType!=returnType)
throw new ArgumentException("return type do not match");
CheckArguments(mi,argumentTypes);
}
public static void CheckArguments(MethodInfo mi, params Type[] argumentTypes)
{
if (mi==null)
throw new ArgumentNullException("mi");
foreach(Type t in argumentTypes)
if (t==null)
throw new ArgumentNullException("argumentType");
ParameterInfo[] pis = mi.GetParameters();
if (pis.Length != argumentTypes.Length)
throw new ArgumentException("number of arguments do not match");
for(int i = 0; i< pis.Length;++i)
{
if (pis[i].ParameterType != argumentTypes[i])
throw new ArgumentException("argument type do not match");
}
}
}
}
|
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.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
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).