【问题标题】:Use internal static class methods from external dll using c#使用 c# 使用来自外部 dll 的内部静态类方法
【发布时间】:2015-02-17 08:51:50
【问题描述】:

我有一个程序集 A,其中的代码类似于

namespace A{
    internal static class SourceData
    {
        static SourceData()
        {    
            //some code
        }   
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static string getdata(string name)
        {
            return (string) null;
        }
    }
}

现在我在我的项目中引用了那个 dll,我想访问 getdata() 方法,有可能吗?如果是,那怎么办? 谢谢。

【问题讨论】:

  • The documentation for internal 有这个例子,表明这是不可能的
  • 如果无法修改程序集,请尝试使用 Reflection
  • 程序集和接口之间有区别@PeterDuniho
  • 程序集和接口之间确实存在差异。所以? 已批准 访问标记为 internal 的程序集内容的方法,无论是接口(如在另一个问题中)还是类中的静态方法(如您的问题)是使用 @ 987654326@。坦率地说,您的问题可能有 几十个 重复;我刚刚选择了最旧的直接适用的答案(即答案与您在此处需要的答案完全相同)。

标签: c# .net dll


【解决方案1】:

在您的程序集 A 中,您可以添加 InternalsVisibleTo 属性: https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute%28v=vs.110%29.aspx

如果要在其他程序集中使用,您当然应该考虑将其公开。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多