Find Attribute from Type - CSharp System

CSharp examples for System:Attribute

Description

Find Attribute from Type

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from  w  ww.  ja  va 2 s . c  om*/

public class Main{
        public static T FindAttribute<T>(this Type type) where T : Attribute
        {
            var attrib = type.GetCustomAttributes(typeof(T), true).FirstOrDefault() as T;
            return attrib;
        }
        public static T FindAttribute<T>(this object obj) where T : Attribute
        {
            if (obj == null)
            {
                return null;
            }

            return obj.GetType().FindAttribute<T>();
        }
}

Related Tutorials