| Property | Value |
|---|---|
| Package | Philips.CodeAnalysis.MaintainabilityAnalyzers |
| Diagnostic ID | PH2143 |
| Category | RuntimeFailure |
| Analyzer | AvoidAssemblyGetEntryAssemblyAnalyzer |
| CodeFix | No |
| Severity | Error |
| Enabled By Default | Yes |
Avoid the method Assembly.GetEntryAssembly(), as it might not give expected results when your code runs under test from a test runner.
Use typeof(xyz).Assembly, where 'xyz' is a class in the intended Assembly.
Code that triggers a diagnostic:
using System.Reflection;
class BadExample
{
public void BadMethod()
{
var assembly = Assembly.GetEntryAssembly();
}
}And the replacement code:
class GoodExample
{
public void GoodMethod()
{
var assembly = typeof(Program).Assembly;
}
}This analyzer does not offer any special configuration. The general ways of suppressing diagnostics apply.