locked
Is programmatic access to type attributes gone?

    Question

  • All the GetCustomAttribute/GetCustomAttributes methods for System.Type appear to be gone. When the language still supports defining your own attributes and uses attributes itself for things like flag enums, thread local fields and MEF, why would the user's ability to query them be left out? Is this functionality gone or has it moved to somewhere I haven't found?

    System.Reflection.CustomAttributeExtensions still exists and gives access to attributes for assemblies, members and parameters, but I can't do it on a class. In the debugger I can see that System.RuntimeType still has attribute support as well but it is internal.

    Tuesday, October 4, 2011 10:44 PM

Answers

  • To elaborate on this and answer the question... TypeInfo derives from MemberInfo (the names don't make much sense that way but alas...).  CustomAttributeExtensions has overloads that take MemberInfo.  So you should be able to get a TypeInfo from your Type via System.Reflection.IntrospectionExtensions.GetTypeInfo and party on that with CustomAttributeExtensions


    Robert Levy
    • Marked as answer by NIGregR Wednesday, October 5, 2011 1:07 PM
    Wednesday, October 5, 2011 1:51 AM

All replies

  • The Type class has changed, and the bulk of the functionality was moved into the TypeInfo class.  Krzystzof Cwalina did an excellent presentation that included a discussion of these changes during //build/. Watch "A .NET developer's view of Windows 8 app development" @  http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-930C for a nice chuk of info on what has been removed or changed.

    See also: http://msdn.microsoft.com/en-us/library/windows/apps/br230302.aspx 
    JWAllison
    Tuesday, October 4, 2011 11:06 PM
  • To elaborate on this and answer the question... TypeInfo derives from MemberInfo (the names don't make much sense that way but alas...).  CustomAttributeExtensions has overloads that take MemberInfo.  So you should be able to get a TypeInfo from your Type via System.Reflection.IntrospectionExtensions.GetTypeInfo and party on that with CustomAttributeExtensions


    Robert Levy
    • Marked as answer by NIGregR Wednesday, October 5, 2011 1:07 PM
    Wednesday, October 5, 2011 1:51 AM
  • Definitely a step backwards from a discoverability standpoint, but at least it is there. Hopefully the extension method to go from Type to TypeInfo and why you might want to do that will be mentioned prominently in the documentation of Type.

    Wednesday, October 5, 2011 1:13 PM
  • The link I posted gives the same info regarding the type.GetTypeInfo() extension (as well as some other significant changes) - but  Krzystof's presentation not only discusses some of the refactoring in 4.5, but the "why's", if you are interested in some of the philosophy/rationale behind these changes to the BCL. 
    JWAllison
    Wednesday, October 5, 2011 1:57 PM