InternalsVisibleTo attribute isn't working
Asked Answered
K

2

7

Before I go on I did go through this InternalsVisibleTo attribute ain’t workin'!

Hence the title

Okay so I had a working version of my project that was using something like the following.

[assembly: InternalsVisibleTo("Stuff.Test.Support, PublicKey="0024000004800000940000000302000000240000525341310004000001000100d158cd56401c3d90b52ca1a5f273d608c3ce12aaa21385b0f4ad7dc1b747e45ee1f1771c104c52cb4da1b587ae38b6d36fa1d8e8f14003c42f700bc62ef2ec04b231c5d930e4bc3691aa1ef7b6713926316d4be1165ede086e94190b44edd4ad0d024230ae6eb9deb728b00d71d1d468b20a9bb78f242bd6c41e640c2e5c0cd5")]

In the Properties/AssemblyInfo.cs file with the internal stuff I want to access. Before checking it in it was working. but after so merging on Team Foundation IDK WTF is wrong but its like my AssemblyInfo.cs doesnt see the attribute but I verified that nothing has changed in the file but still I get the "innaccessable due to its protection level." error. (Gama Radiation? Sunspot? The little evil gnomes that train bugs to tweek working code into not working code lol)

I have tried just adding the individual instances using reflection...

Assembly Core = Assembly.LoadFile("C:\Stuff.Internal.Core\bin\Debug\Stuff.Internal.Core.dll");

Object AssmWithIdentifier = Core.CreateInstance("AssemblyWithIdentifer", false);

When I attempt this I get 'Stuff.Internal.AssemblyWithIdentifer' is inaccessible due to protection level.

Otherwise if I comment out anything having to do with the internal stuff my project compiles just fine.

I double checked my "PublicKey" with Red Gates Reflector

and I have also tried using privateObjects to possibly access methods that way but no matter how I try i can't instantiate anything internal.

I have also tried simply adding a new project, compiling it any referencing and yes that actually worked but after that I attemped to create a new project and add all the stuff in "Stuff.Test.Support" project to include references and the like and add that to the AssemblyInfo.cs that in in the project that I wish to access but, once I attempt to compile I get the same error "innaccessable due to its protection level."

Edit: I did forget to mention I am using a sharedAssembly.cs that globally manages the assemblies and has several attributes set up in a similar fashion to the example assembly: InternalsVisibleTo attribute I have shown at the beginning of this question. I am also using Strong Named assemblies if that is something not made prominent by my example.

Kidwell answered 17/8, 2010 at 15:41 Comment(2)
Hey, maybe you should remove any workflow definitions from that project! Try that out.Bury
OMG that works. Maybe you should put that down as an answer so you can actually get some cred for it.Kidwell
K
4

Turns out workflow definitions were causing the discrepancy. Still trying to figure out why though.....
Well if anyone can provide a link to something explaining why will get +1 from me.

Kidwell answered 18/8, 2010 at 17:4 Comment(1)
I removed all the workflow defs in the project. Not the best answer but it works now. And there is currently a bug report to Microsoft for it. And the link is connect.microsoft.com/VisualStudio/feedback/details/588710/…Kidwell
G
2

1) if InternalsVisibleTo is set up correctly, you shouldn't need reflection to instantiate them from the 'friend', it can just use ctor's and the like directly. I would go this route so that the VS IDE can give you faster feedback on whether the internals are really visible to the target project.

2) as per the error message you got later, have you tried InternalsVisibleTo("Stuff.Test.Support") ?

Garnett answered 17/8, 2010 at 16:32 Comment(1)
1)I have tried using reflection as an alternative to using InternalsVisibleTo since it is not currently working. I'm currently tinkering with reflection and seeing if there is something I missed. 2) I am currently using strong-named assemblies so I get the error Friend assembly reference 'Stuff.Tests.Support' is invalid. Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations. Sorry I just modded my question to divulge a bit more info about my setup. So maybe that could help you better.Kidwell

© 2022 - 2024 — McMap. All rights reserved.