Cant access C# static class

1k Views Asked by At

I am having trouble implementing an answer I got here!, Can anyone help me access this private static class?

namespace VEParameterTool
{
    class ProcessorPlugIn
    {
        private static class UnsafeNativeMethods
        {
            [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)]
            static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName);

            [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
            static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

            [DllImport("kernel32", SetLastError = true)]
            static extern bool FreeLibrary(IntPtr hModule);
        }      

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private delegate ErrorCode ProcessorFunction(ref IntPtr pRx);

        IntPtr hLib = UnsafeNativeMethods.LoadLibrary("Processor.dll");
    }
}

When I try to LoadLibrary, I get the error about protection levels:

'VEParameterTool.ProcessorPlugIn.UnsafeNativeMethods.LoadLibrary(string)' is inaccessible due to its protection level

I have searched for the solution but cant see anything to do with static classes.

Any hep would be greatly appreciated.

Andy

2

There are 2 best solutions below

0
On BEST ANSWER

You can leave the inner class private, making it only accessible to the ProcessorPlugIn class, but you have to make the methods public.

private static class UnsafeNativeMethods
{
    [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)]
    public static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName);

    [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

    [DllImport("kernel32", SetLastError = true)]
    public static extern bool FreeLibrary(IntPtr hModule);
}

These methods will be only accessible from where their containing class can be accessed, in this example, ProcessorPlugIn.

0
On

Why are you attempting to declare a private static class? The only method I can think of potentially being able to access information within the class is if you were to make it a nested private static class with public methods - and based on the code posted that is not the case.

Private means just that - it cannot be accessed from outside. Read more about access modifiers here: http://msdn.microsoft.com/en-us/library/ms173121.aspx

See here for an example of a private static class implementation:

https://dotnetfiddle.net/Lyjlbr

using System;

public class Program
{
    public static void Main()
    {
        Bar.DoStuff();

        // Bar.DoOtherStuff(); // Cannot be done due to protection level

        Bar.DoStuffAndOtherStuff(); // note that this public static method calls a private static method from the inner class
    }

    private static class Bar
    {
        public static void DoStuff()
        {
            Console.WriteLine("Test");
        }

        public static void DoStuffAndOtherStuff()
        {
            DoStuff();
            DoOtherStuff();
        }

        private static void DoOtherStuff()
        {
            Console.WriteLine("other stuff");
        }
    }
}

EDIT: apparently you can also use reflection to access private classes/members/functions, but I don't know much about it. See here: Why can reflection access protected/private member of class in C#?