using System; using System.Linq; using System.Reflection; class MyClass { public void Method1() {} private void Method2() {} public void Method3(int x, string y) {} } class Program { static void Main() { Type type = typeof(MyClass); MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (MethodInfo method in methods) { Console.WriteLine(method.Name); } } }
using System; using System.Linq; using System.Reflection; class MyClass { [Obsolete("This method is deprecated.")] public void Method1() {} public void Method2() {} } class Program { static void Main() { Type type = typeof(MyClass); MethodInfo method = type.GetMethod("Method1"); object[] attributes = method.GetCustomAttributes(true); foreach (object attribute in attributes) { Console.WriteLine(attribute.ToString()); } } }This code uses the ISymbol interface to get the custom attributes of the Method1 method of the MyClass class and print their names. The method GetCustomAttributes() returns an array of objects representing the custom attributes applied to the method. Package library: System.Reflection.