Compiler Error CS0122

'member' is inaccessible due to its protection level

The access modifier for a class member prevents accessing the member. For more information, see Access Modifiers.

Extension methods cannot access private members of the type they are extending.

One cause of this (not shown in the sample below) can be omitting the /out compiler flag on the target of a friend assembly. For more information, see Friend Assemblies and OutputAssembly (C# Compiler Options).

Example

The following sample generates CS0122:

// CS0122.cs
public class MyClass
{
    private int data;

    void PrivateMethod() {}
    public void PublicMethod() {}
}

public static class MyClassExtensions
{
    public static int GetData(this MyClass myClass)
    {
        return myClass.data;   // CS0122
    }
}

public class Program
{
    public static void Main()
    {
        MyClass a = new MyClass();
        a.PrivateMethod();   // CS0122
        a.PublicMethod();   // OK
    }
}