Skip to content

Latest commit

 

History

History
50 lines (38 loc) · 1.36 KB

File metadata and controls

50 lines (38 loc) · 1.36 KB

PH2143: Avoid Assembly.GetEntryAssembly()

Property Value
Package Philips.CodeAnalysis.MaintainabilityAnalyzers
Diagnostic ID PH2143
Category RuntimeFailure
Analyzer AvoidAssemblyGetEntryAssemblyAnalyzer
CodeFix No
Severity Error
Enabled By Default Yes

Introduction

Avoid the method Assembly.GetEntryAssembly(), as it might not give expected results when your code runs under test from a test runner.

How to solve

Use typeof(xyz).Assembly, where 'xyz' is a class in the intended Assembly.

Example

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;
    }
}

Configuration

This analyzer does not offer any special configuration. The general ways of suppressing diagnostics apply.