Reading custom attributes in ReflectionOnly mode
-
Does anyone know a way to get the type names of custom attributes applied to a class when the assembly is loaded in ReflectionOnly mode? The only method to get custom attributes that I know is Type.GetCustomAttributes; but since that returns attribute instances and not just the type name, it can't be used in ReflectionOnly mode. I just want to make sure I'm not overlooking something before sending a suggestion to Microsoft.
-
Does anyone know a way to get the type names of custom attributes applied to a class when the assembly is loaded in ReflectionOnly mode? The only method to get custom attributes that I know is Type.GetCustomAttributes; but since that returns attribute instances and not just the type name, it can't be used in ReflectionOnly mode. I just want to make sure I'm not overlooking something before sending a suggestion to Microsoft.
Daniel Grunwald wrote: but since that returns attribute instances and not just the type name, it can't be used in ReflectionOnly mode. It returns instances of the attributes. You can think attributes being instantiated at compile time. You can always call object.GetType() on those returned instances. And what is ReflectionOnly mode? :confused: xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
-
Daniel Grunwald wrote: but since that returns attribute instances and not just the type name, it can't be used in ReflectionOnly mode. It returns instances of the attributes. You can think attributes being instantiated at compile time. You can always call object.GetType() on those returned instances. And what is ReflectionOnly mode? :confused: xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
ReflectionOnly mode means the type is loaded in "inspection context". That is a .NET 2.0 feature to load assembly if you just want to reflect on them without creating types. It's faster, doesn't lock the assembly file etc... But you can't get instances from ReflectionOnly assemblies, so GetCustomAttributes() always results in an exception. But I just found the solution myself: CustomAttributeData.GetCustomAttributes(type)[^] gets information about the attributes in a ReflectionOnly compatible way.