HBoskugelS
(Holger Boskugel)
June 20, 2025, 9:51am
1
SonarQube Server: Community Build v25.5.0.107428
No information available about configuration, not maintained by us.
I have the problem, that SonarQube reports the S3885 issue, which can’t be solved as we need to load an Assembly
when .NET can’t detect it by name or reference.
I came then across this issue :
opened 02:39AM - 28 Apr 21 UTC
closed 11:45AM - 04 Jun 21 UTC
### Description
I have code thats loading an assembly the user directs from d… isk that then does stuff. This requires use of `Assembly.LoadFrom(string pathToAssembly)` as that is how to specify a file system path. SonarLint is telling me I should use `Assembly.Load(string assemblyString)` instead, but that doesn't accept a path, it expects an assembly name. That simply wont work.
Here is a visual depiction of the difference
``` csharp
Assembly.LoadFrom(@"c:\temp\Toolbox.dll");
Assembly.Load("Toolbox.dll, version=1.1.10.1220, locale=en, publickey=1234567890123456789012345678901234567890")
```
The [RSPEC ](https://jira.sonarsource.com/browse/RSPEC-3885)could not be any more incorrect in this scenario.
> Use another method, and you might end up with a dll other than the one you expected.
If I am loading the assembly from a specific path on disk, then I am loading the dll that I expected. If I use `Assembly.Load(string assemblyName)`, the runtime is going to load the first assembly that matches the name spec given, which may not actually be the one I expect. It could be pulling from the GAC or another folder entirely from where the one is that I want to load. **It** has the chance of loading an assembly I didnt expect, not `LoadFrom` or `LoadFile`.
`LoadFile` and `LoadFrom` are really no different from the `Assembly.Load(byte[] assemblyBytes, )` overloads, which also cant load an unexpected assembly. This rule is fine for `LoadWithPartialName` but because that is being deprecated, not because it will load an unexpected assembly.
### Expected behavior
I expect it to not report this usage as incorrect.
I also expect it to not give non-working advice
### Actual behavior
It does the opposite of both expectations
We also use the AssemblyResolve event to load assemblies from another path. So we want do this by Assembly.LoadFrom
, but this you report as issue.
How can it be solved?
Colin
(Colin)
June 20, 2025, 1:08pm
2
Hey @HBoskugelS !
Thanks for bringing up this topic. Can you share some code samples?
Hey SonarSource Community!
False-positives happen , as do false-negatives, and we’re eager to fix them. We are thrilled when our users report problems, so we can make our products better.
What is a false-positive (FP)?
A false-positive is when an issue is raised unexpectedly on code that should not trigger an issue, or where the suggested action doesn’t make any sense for the code.
What is a false-negative (FN)?
A false-negative is when an issue should be raised on a piece of code, but isn’t.
…
HBoskugelS
(Holger Boskugel)
June 20, 2025, 1:23pm
3
It reports the code at return
in OnAssemblyResolve
private static Assembly OnAssemblyResolve(Object sender,
ResolveEventArgs args)
{
String[] nameParts = args.Name.Split(',');
String assemblyFilePath = __librariesDirectoryPaths.SelectMany(ldp => new[]
{
Path.Combine(ldp,
$"{nameParts[0]}.dll"),
Path.Combine(ldp,
$"{nameParts[0]}.exe")
})
.FirstOrDefault(afp => File.Exists(afp));
if(assemblyFilePath == null)
{
return null;
}
return Assembly.LoadFrom(assemblyFilePath);
}
static DllExports()
{
AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
}
Hi @HBoskugelS ,
Thanks for raising this!
I agree this is a False Positive.
The MSDN explicitly calls this out as what not to do !
I have added a repro to our codebase and a ticket to not raise this rule inside event handlers.
HBoskugelS
(Holger Boskugel)
June 26, 2025, 3:57pm
8
Please look right what MSDN tells in the article. The first sample is what you have NOT to do. The second then shows the solution with the LoadFile
. But this Method is deprecated, so LoadFrom
is the right one.
Hi @HBoskugelS ,
Yes, I realise I was unclear.
To be clear:
In the code snippet you provided, we should not suggest using Assembly.Load() as it could lead to a stack overflow. (This is what the MSDN calls out as bad practice and what I have added a reproducer in our codebase for).
In your code the use of Assembly.LoadFrom is correct.
I would recommend marking the code as a False Positive in SonarQube Server.
Thanks