54

Is it possible to make internal classes from my assembly visible to other assemblies?

I know about the AssemblyInfo file and the [assembly: InternalsVisibleTo()] attribute, but it doesn't work in my case.

The main purpose is to make it possible to call methods from LINQPAD, so this [assembly: InternalsVisibleTo("LINQPad")] doesn't work. I don't know why. In my project, I'm using dependency resolver, and it is hard to do such a thing in LINQPAD. Any suggestions?

Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
Neir0
  • 12,849
  • 28
  • 83
  • 139
  • 4
    I strongly suspect that LINQPad is generating assemblies dynamically - it's probably not the LINQPad assembly itself that needs access to your types. – Jon Skeet Jan 16 '13 at 08:38
  • @Jon Skeet Autocomplete doesnt work too. I guess it LINQPAD assembly. – Neir0 Jan 16 '13 at 08:40
  • 4
    Autocomplete is almost certainly based on reflection - I would fully expect it to be filtering to public properties only. After all, the LINQPad assembly itself doesn't contain *direct* calls to your code, does it? – Jon Skeet Jan 16 '13 at 08:42
  • @Jon Skeet you are right...ok what is the solution? – Neir0 Jan 16 '13 at 08:53
  • Why not just make the classes public? What is the use case for letting a user write code in LINQPad but not directly compile against your assemblies? – Mike Zboray Jan 16 '13 at 09:03
  • @mike z because of these classes from a big project where internal modifier required. – Neir0 Jan 16 '13 at 09:07
  • 1
    Sounds like you should be contacting Joe Albahari with a feature request... – Jon Skeet Jan 16 '13 at 09:15

2 Answers2

78

I've just uploaded a new beta that allows this to work.

Add the following attribute to the libraries whose internals you want LINQPad to access:

[assembly: InternalsVisibleTo("LINQPadQuery")]

You'll also need to enable this feature in LINQPad's preferences (Edit | Preferences | Advanced).

Let me know how you get along.

Joe Albahari
  • 30,118
  • 7
  • 80
  • 91
  • Works for me. There have been times when I have had to revert to reflection to access internal methods, so this is will be a handy feature, so Thank you. – sgmoore Jan 19 '13 at 14:41
  • The warning about the access level is gone for me too, but now I get this: `Inconsistent accessibility: base class 'Database.Model.CustomDataContext' is less accessible than class 'UserQuery'`. Do you know how i can get around this? The signature of CustomDataContext is `internal partial class CustomDataContext : DbContext` – SeriousM Jun 21 '13 at 11:47
  • 1
    I tried the version v4.46.11 and it works!! thank you so much :) – SeriousM Jul 18 '13 at 14:20
  • I was searching for visual studio - nevertheless this answer worked for me. – kevin Sep 16 '14 at 08:29
  • What about the signed assemblies? Is there any possibility? – Beniamin Mar 03 '15 at 09:56
8

You can also go to the project properties into AssemblyInfo.cs file and set it there.

For example:

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")]
Ajit Panigrahi
  • 752
  • 10
  • 27
Eli Syrota
  • 81
  • 1
  • 3