using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace Sb2.Extensions
{
public static class ReflectionExtensions
{
/// <summary>
/// Invokes the method.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj">The obj.</param>
/// <param name="methodName">Name of the method.</param>
/// <returns></returns>
public static T InvokeMethod<T>(this object obj, string methodName)
{
return InvokeMethod<T>(obj, methodName, null);
}
/// <summary>
/// Invokes the method.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj">The obj.</param>
/// <param name="methodName">Name of the method.</param>
/// <param name="args">The args.</param>
/// <returns></returns>
public static T InvokeMethod<T>(this object obj, string methodName, object[] args)
{
Type type = obj.GetType();
MethodInfo methodInfo = type.GetMethod(methodName);
if (methodInfo != null)
{
return (T)methodInfo.Invoke(obj, args);
}
return default(T);
}
}
}
|