Contains Attribute by Method - CSharp System.Reflection

CSharp examples for System.Reflection:MethodInfo

Description

Contains Attribute by Method

Demo Code


using System.Reflection;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*  w  w  w  .  ja  va 2s .c  om*/

public class Main{
        public static T ContainsAttributebyMethod<T>(Type type, string methodName) where T : Attribute
        {
            MethodInfo mi = type.GetMethod(methodName);
            if (mi != null)
            {
                object[] arr = mi.GetCustomAttributes(typeof(T), true);
                if (arr.Length == 0) return null;
                return (T)arr[0];
            }
            return null;
        }
}

Related Tutorials