dimanche 13 décembre 2015

Missing Custom Attriubutes

In C#, one can use reflection to obtain the attributes on a member m via m.CustomAttributes (see the documentation for this property). However, this approach seems to miss three custom attributes on the GetType method of Object (see the Object in the Reference Source for .NET Framework 4.6.1):

using System;
using System.Linq;
namespace ConsoleApplication1 {
  public class Program {
    public static void Main(string[] args) {
      var desiredCustomAttributes = typeof(object)
        .GetMethods()
        .First(m => m.Name == "GetType")
        .CustomAttributes
        .Select(ca => ca.ToString())
        .Where(s =>
          s.Contains("Pure") ||
          s.Contains("ResourceExposure") ||
          s.Contains("MethodImplAttribute"));
      var n = desiredCustomAttributes.Count();
      Console.WriteLine("Expected: 3");
      Console.WriteLine("  Actual: " + n); // prints "  Actual: 0"
      Console.ReadKey();
    }
  }
}

Why do these three custom attributes not show up?

Maybe it has something to do with the fact that it is an external method?





Aucun commentaire:

Enregistrer un commentaire