【问题标题】:Cant access C# static class无法访问 C# 静态类
【发布时间】:2014-11-11 22:32:48
【问题描述】:

我在执行我在这里得到的答案时遇到问题!,谁能帮我访问这个私有静态类?

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

当我尝试 LoadLibrary 时,我收到有关保护级别的错误:

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

我已经搜索了解决方案,但看不到与静态类有关的任何内容。

我们将不胜感激。

安迪

【问题讨论】:

  • 将您的类/方法声明为公开可能会有所帮助。
  • Andy 您是否熟悉类的访问级别,例如 Private, Public, Protected Sealed...etc 您是否阅读过 C# 中的封装部分以及访问修饰符级别..?
  • 不,现在真的...您是否尝试通过 COM 导入 dll 并将其用作静态类?嗯....不。
  • 你为什么要对我回答的合法问题投反对票,我正在动态加载一个 dll,现在它工作得很好,这就是下面的完美答案。你不明白这个问题,我还说这是你开始侮辱之前的一个回答。答案是私有静态类中缺少 public 关键字。
  • @Andy 有时似乎社区在反对票上很快,但在赞成票上却没有那么多。然而,downvote 按钮的工具提示确实列出了“缺乏任何研究” - 对您的错误消息进行快速谷歌搜索会(似乎)阐明您的问题。此外,原始问题的链接可能是个好主意。

标签: c# dynamic-loading


【解决方案1】:

你为什么要声明一个私有静态类?我能想到的唯一可能访问类中信息的方法是,如果您要将其设置为具有公共方法的嵌套私有静态类 - 并且根据发布的代码,情况并非如此。

私人意味着——它不能从外部访问。在此处阅读有关访问修饰符的更多信息:http://msdn.microsoft.com/en-us/library/ms173121.aspx

查看这里的私有静态类实现示例:

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

编辑:显然您也可以使用反射来访问私有类/成员/函数,但我对此知之甚少。见这里:Why can reflection access protected/private member of class in C#?

【讨论】:

    【解决方案2】:

    您可以保留内部类private,使其只能由ProcessorPlugIn 类访问,但您必须使方法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);
    }
    

    这些方法只能从可以访问它们的包含类的地方访问,在本例中为ProcessorPlugIn

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 2016-06-14
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      相关资源
      最近更新 更多