【问题标题】:Make internal classes visible to other assemblies使内部类对其他程序集可见
【发布时间】:2013-01-16 08:36:06
【问题描述】:

是否可以使我的程序集中的内部类对其他程序集可见?

我知道 AssemblyInfo 文件和 [assembly: InternalsVisibleTo()] 属性,但在我的情况下它不起作用。

主要目的是让从 LINQPAD 调用方法成为可能,所以这个[assembly: InternalsVisibleTo("LINQPad")] 不起作用。我不知道为什么。在我的项目中,我使用了依赖解析器,在 LINQPAD 中很难做到这一点。有什么建议吗?

【问题讨论】:

  • 我强烈怀疑 LINQPad 正在动态生成程序集 - 可能不是 LINQPad 程序集本身需要访问您的类型。
  • @Jon Skeet 自动完成功能也不起作用。我猜它是 LINQPAD 程序集。
  • 自动完成几乎肯定是基于反射的——我完全希望它只过滤公共属性。毕竟,LINQPad 程序集本身不包含对代码的直接调用,是吗?
  • @Jon Skeet 你是对的...好的解决方案是什么?
  • 听起来您应该联系 Joe Albahari 提出功能请求...

标签: c# linqpad internals


【解决方案1】:

我刚刚上传了a new beta,它允许这个工作。

将以下属性添加到您希望 LINQPad 访问其内部的库:

[assembly: InternalsVisibleTo("LINQPadQuery")]

您还需要在 LINQPad 的首选项(编辑 | 首选项 | 高级)中启用此功能。

让我知道你们相处得怎么样。

【讨论】:

  • 为我工作。有时我不得不恢复到反射来访问内部方法,所以这将是一个方便的功能,所以谢谢。
  • 关于访问级别的警告对我来说也消失了,但现在我明白了:Inconsistent accessibility: base class 'Database.Model.CustomDataContext' is less accessible than class 'UserQuery'。你知道我怎么能解决这个问题吗? CustomDataContext 的签名是internal partial class CustomDataContext : DbContext
  • 我尝试了 v4.46.11 版本,它可以工作!!非常感谢你:)
  • 我正在寻找视觉工作室 - 不过这个答案对我有用。
  • 签名的程序集怎么样?有没有可能?
【解决方案2】:

您也可以将项目属性转到 AssemblyInfo.cs 文件中并在那里进行设置。

例如:

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("*****)]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("*****")]
[assembly: AssemblyProduct("*****")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: InternalsVisibleTo("[HERE IS YOUR ASSEMBLY WHERE YOU WANT TO SEE INTERNALS OF CURRENT ASSEMBLY")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("6fbd2c14-031d-48cc-9cc6-f1b85d701e89")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

【讨论】:

  • 我发现这比接受的答案更有帮助,因为它说明了将这些信息放在哪里:AssemblyInfo.cs
猜你喜欢
  • 2017-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-26
  • 1970-01-01
  • 1970-01-01
  • 2015-03-01
相关资源
最近更新 更多