Reputation: 362
GetCustomAttribute cannot find attribute on an assembly loaded via "Assembly.LoadFrom(path)" in custom AssemblyLoadContext.
The load flow:
Assembly Y has an assembly-level attribute on it. The attribute comes from a shared assembly (TechTalk.SpecFlow) which is staticaly linked to both X and Y.
When I try to get the attribute via type-version of Attribute.GetCustomAttribute
it returns null.
When I list all attributes via CustomAttributes
property then the attribute is there and it has exactly the same assembly fullname.
If I try to cast it to staticaly known type, then strange exception appears:
[A]TechTalk.SpecFlow.Plugins.RuntimePluginAttribute cannot be cast to [B]TechTalk.SpecFlow.Plugins.RuntimePluginAttribute. Type A originates from 'TechTalk.SpecFlow, Version=3.1.0.0, Culture=neutral, PublicKeyToken=0778194805d6db41' in the context 'Default' at location 'c:\Users\yapaxi\source\repos\IntegrationTests\IntegrationTests.ChargingTokens\bin\Debug\netcoreapp3.1\TechTalk.SpecFlow.dll'. Type B originates from 'TechTalk.SpecFlow, Version=3.1.0.0, Culture=neutral, PublicKeyToken=0778194805d6db41' in the context 'Default' at location 'c:\Users\yapaxi\source\repos\IntegrationTests\IntegrationTests.ChargingTokens\bin\Debug\netcoreapp3.1\TechTalk.SpecFlow.dll'.
For some reason it thinks that both assemblies are loaded to the Default
context, which is odd, because all loading happens in the custom assembly loading context.
If I try AssemblyLoadContext.CurrentContextualReflectionContext.Name
before Assembly.LoadFrom
it shows the name of the custom loading context - as expected.
Why does it still think that assemblies are loaded to Default context? The main application does not even have static references to those assemblies - it does not have any custom references at all.
If I run assembly X directly (without AssemblyLoadContext) it all works well.
Env:
Upvotes: 3
Views: 1627
Reputation: 938
Maybe this article can help you to find out the issue. Especially the section where it explains how to debug Type conversion issues with shared dependencies. The problem is that the default debugger output does not give you enough information to resolve that kind of error.
Upvotes: 0