开发者

Using Reflection for finding deprecation

I am curious if anyone has written any code to reflect into a class and find its Deprecated methods?

Ive whipped a T4 开发者_如何学编程Template for reactive and would love to have it stop generating handlers for deprecated events, any clever hackers out there that already beat me to the punch?


I don't know if you're asking for t4 framework or not but here's a generic reflection sample for Obsolete flagged methods.

class TestClass
{
    public TestClass()
    {
        DeprecatedTester.FindDeprecatedMethods(this.GetType());
    }

    [Obsolete("SomeDeprecatedMethod is deprecated, use SomeNewMethod instead.")]
    public void SomeDeprecatedMethod() { }

    [Obsolete("YetAnotherDeprecatedMethod is deprecated, use SomeNewMethod instead.")]
    public void YetAnotherDeprecatedMethod() { }

    public void SomeNewMethod() { }        
}

public class DeprecatedTester
{
    public static void FindDeprecatedMethods(Type t)
    {
        MethodInfo[] methodInfos = t.GetMethods();

        foreach (MethodInfo methodInfo in methodInfos)
        {
            object[] attributes = methodInfo.GetCustomAttributes(false);

            foreach (ObsoleteAttribute attribute in attributes.OfType<ObsoleteAttribute>())
            {
                Console.WriteLine("Found deprecated method: {0} [{1}]", methodInfo.Name, attribute.Message);
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜