My project references TypesDefinitionAssembly
with type SomeType
, which is marked by attributes XSerializationOptions
from XSerializationLibrary
and YSerializationOptions
from YSerializationLibrary
.
Obviously, to check whether SomeType
is marked by XSerializationOptions
, I need to reference XSerializationLibrary
as well. However, I don't want to reference YSerializationLibrary
(which might even not be available).
Currently, the call to
typeof(SomeType).IsDefined(typeof(XSerializationOptions))
fails because IsDefined
, for some reason, walks through all the attributes and tries to resolve all their types. The exception looks like:
System.IO.FileNotFoundException: Could not load file or assembly 'YSerializationLibrary, Version=1.2.3.4, Culture=neutral, PublicKeyToken=0123456789abcdef' or one of its dependencies. The system cannot find the file specified.
at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable)
at System.Reflection.CustomAttribute.IsDefined(RuntimeType type, RuntimeType caType, Boolean inherit)
Is it possible someway to workaround this problem? How do I check whether XSerializationOptions
is defined on SomeType
without referencing a completely irrelevant YSerializationLibrary
?
The problem becomes even worse once you consider that XSerializationLibrary
itself calls to Enum.IsDefined
; and, as such, it becomes impossible to use SomeType
for serialization with XSerializationLibrary
unless you also reference YSerializationLibrary
.