Skip to content

Decompiled code compilation error accessing protected members due to unnecessary casting #3728

@LazaroOnline

Description

@LazaroOnline

Input code

var cat = new Cat();
cat.Test();

public class Cat : Animal
{
	public void Test()
	{
		var n = this._myProtectedField; // Correct way to access, without casting.
		Console.WriteLine("Test: " + n);
	}
}

public abstract class Animal
{
	protected int _myProtectedField = 2;
}

Erroneous output

var n = ((Animal)this)._myProtectedField  // Error: not accessible like this.

Compiler error message:
Cannot access protected member 'Animal ._myProtectedField ' via a qualifier of type 'Animal'; the qualifier must be of type 'Cat' (or derived from it)

Details

Occurs in both the vscode extension and the stand alone app:
ILSpy version 10.0.1.8346+aad16c66e96eb887eb05887d6b5a9e0522637906
icsharpcode.ilspy-vscode v0.24.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDecompilerThe decompiler engine itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions