追踪方法上的特性!

public static T GetMethodAttr<T>(bool whole = true) where T : Attribute
        {
            T res = null;
            StackTrace st = new StackTrace(true);
            for (int i = 0 ; i < st.FrameCount ; i++)
            {
                StackFrame sf = st.GetFrame(i);
                var attrs = sf.GetMethod().GetCustomAttributes(typeof(T), false);
                if (attrs.Length > 0)
                {
                    res = attrs[0] as T;
                    if (!whole) return res;
                }
            }
            return res;
        }

说明:whole = true 追踪最外层的!!

原文地址:https://www.cnblogs.com/zhuwansu/p/11134381.html