We all know that the assembly can be queried for attributes using the GetCustomAttributes method. I want to use this to identify an extension module for my application. However, to avoid loading every assembly I prefer a defensive approach:
using Assembly.ReflectionOnlyLoadFrom to get more details about an assembly (has it my ModuleAttribute?)
if the ModuleAttribute is found, I will finally load it using Assembly.LoadFrom
Unfortunately it seems that there is no way to get the attributes from an assembly, that is loaded into the reflection-only context:
myAssembly.GetCustomAttributes(typeof(ModuleAttribute), false)
fails with an InvalidOperationException
It is illegal to reflect on the custom attributes of a Type loaded via ReflectionOnlyGetType
and
CustomAttributeData.GetCustomAttributes(myAssembly)
fails with ReflectionTypeLoadException
because of dependant assemblies not being loaded.
So how to get the attributes without
?
Right-click on a user, then click Properties. Click the Attribute Editor tab, then confirm that the custom attribute you created is listed in the "Attribute" column (e.g., LastPassK1).
Which of the following are correct ways to specify the targets for a custom attribute? A. By applying AttributeUsage to the custom attribute's class definition.
Assembly identity attributes Three attributes, together with a strong name (if applicable), determine the identity of an assembly: name, version, and culture. These attributes form the full name of the assembly and are required when referencing the assembly in code.
A custom attribute is a property that you can define to describe assets. Custom attributes extend the meaning of an asset beyond what you can define with the standard attributes. You can create a custom attribute and assign to it a value that is an integer, a range of integers, or a string.
It is possible to load (reflection only) assemblies with dependencies.
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve +=
(s, e) => Assembly.ReflectionOnlyLoad(e.Name);
would fix failures when loading, assuming they are all in the same folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With