C
C#2y ago
an.c.

✅ Can't get `InternalsVisibleTo` to work

From assembly (project) A.B I added the following in a file
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("A.B.Tests")]

namespace MyNamespace
{
internal class MyClass {}
}
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("A.B.Tests")]

namespace MyNamespace
{
internal class MyClass {}
}
But the assembly A.B.Test can't see MyClass. Is there something extra that I should do?
46 Replies
ero
ero2y ago
Nope, that should do it
an.c.
an.c.2y ago
So what you recommend at this point is for me to make extra sure that the names of the assemblies are correct, or what?
ero
ero2y ago
i recommend sharing the error
an.c.
an.c.2y ago
I just disassembled A.B and I can see class Myclass in it but should I also see that instruction for the CompilerService? The error is quite simple:
CS0234 The type or namespace name 'MyClass' does not exist in the namespace 'MyNamesapce' (are you missing an assembly reference?) A.B.Tests (net472), A.B.Tests (netcoreapp3.1)
CS0234 The type or namespace name 'MyClass' does not exist in the namespace 'MyNamesapce' (are you missing an assembly reference?) A.B.Tests (net472), A.B.Tests (netcoreapp3.1)
ero
ero2y ago
incompatible frameworks i guess?
ero
ero2y ago
an.c.
an.c.2y ago
you could be onto something A.B.Tests is a netstandard2.0 proj. Shouldn't that be the most compatible of them all, though?
ero
ero2y ago
i don't know. why is it that version? forced to by your company?
an.c.
an.c.2y ago
yes, correct it has to be portable and part of a lib and bla bla bla so I don't have a say on netstandard2.0 or not
ero
ero2y ago
hm, cringe net core is the most portable i mean, the entire point of it is to be cross platform
an.c.
an.c.2y ago
let me rephrase: I don't know why but it's set netstandard2.0 so forget what I said I don't recall the exact reason
ero
ero2y ago
i can't test that setup because i'm on linux
an.c.
an.c.2y ago
how sure are you that's the framework compatibility an issue here?
ero
ero2y ago
like 10%? i don't know much about it
an.c.
an.c.2y ago
can't linux target netstandard?
ero
ero2y ago
ask in #roslyn but not framework and you included A.B.Tests (net472) this so
an.c.
an.c.2y ago
I can temporarily remove framework but you can see that the issue is also for core
ero
ero2y ago
i actually can't see that, no
an.c.
an.c.2y ago
A.B.Tests (net472), A.B.Tests (netcoreapp3.1)
ero
ero2y ago
in fact i showed that the issue does not exist
an.c.
an.c.2y ago
the error I posted this
ero
ero2y ago
oh, i mustve gotten confused
an.c.
an.c.2y ago
np
ero
ero2y ago
yeah then i'm definitely more sure that it's the target frameworks
an.c.
an.c.2y ago
I see
ero
ero2y ago
make sure to ask in the roslyn channel, they're sure to know more
an.c.
an.c.2y ago
thank you
333fred
333fred2y ago
It's the most compatible library type. Your tests can't be that though
an.c.
an.c.2y ago
My tests are infact net472 and netcoreapp3.1 netstandar2.0 is the assembly where the internal class is defined. And I need access to it from the tests
333fred
333fred2y ago
Can you post a repro?
an.c.
an.c.2y ago
🙂
333fred
333fred2y ago
Why the smiley?
an.c.
an.c.2y ago
I'll try a repro proj tomorrow morning (CET) that'll likely work
333fred
333fred2y ago
Well, the other things to check: make sure the assembly name is correct
an.c.
an.c.2y ago
and I'll be back at "it's just somethign else in my proj" that's why the smiley
333fred
333fred2y ago
Build the test project, and verify that the dll has the assembly name you expect
an.c.
an.c.2y ago
I checked the names 4 times
333fred
333fred2y ago
Not file name Assembly name
an.c.
an.c.2y ago
I looked inside the dll and verified that all is where I expect it to be and properly named
333fred
333fred2y ago
The other thing to check is whether strongnaming is involved If a.b is strongnamed, a.b.test must also be strongnamed
an.c.
an.c.2y ago
Not file name Assembly name
I'll check exactly the diff here once the dll is loaded in a disassembler (if this is the name), I assume that the name shown there is the name of the assembly am I wrong in assuming this? I'll look into the lib being strong-named
333fred
333fred2y ago
Look at the assembly info section of your disassembler
an.c.
an.c.2y ago
the name looks correct
333fred
333fred2y ago
Then a.b is likely being strongnamed
an.c.
an.c.2y ago
I understand. Thank you for following up. I'll keep investigating this was not a repro project. I was just meant to give an idea of what I was doing. but I'll check naming again my issue was a funny one not really related to this working or not I had the same namespace (by name) in both assembly and so MyClass was not found in MyNamespace because MyNamespace was not the one from the external assembly, but it was fetch from the same assembly. An extern alias fixed my issues. Thank you all for the support.
Accord
Accord2y ago
Closed!