4

Lately I found a few tools that allow the .Net Library to be merged within the windows application.

Now the real question is how does the behavior of the library changes,

  1. Does the internal class remain internal to library? Or does it become internal to the application it's been merged with?
  2. Are there chances that library will malfunction?

Extending question:

  1. Won't it be better that when merging the assembly that are internal should be made private so that that can't be used by the application they are merged in?
Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110

4 Answers4

6

Classes can't be private unless they're nested.

But consider this: if you're merging assemblies A and B, then you must have already compiled them before merging. When they were compiled, the internal methods of each were inaccessible to the other. Therefore, in the merged code, there could be no method that calls internal methods of the other assembly.

Wont it be better that when merging the assembly that are internal should be made private so that that cant be used by the application they are merged in?

How would that work? If a top-level type were private, it would be accessible to no other types at all. That's why you can't define private types (unless they're nested within another type).

Suppose assembly A has classes C and D, where C is internal, and D calls some method from class C. When class C is made private (in some hypothetical version of the CTS where this is possible), class D breaks.

phoog
  • 42,068
  • 6
  • 79
  • 117
  • what about "Reflexil" it can make that possible ?, editing IL byte code – Parimal Raj Dec 26 '12 at 14:41
  • 1
    @AppDeveloper if you edit the IL code, then, presumably, yes, internal methods defined in one of the source assemblies would be accessible to methods defined in the other source assembly. After you merge the source assemblies, the resuling assembly is, well, a single assembly. – phoog Dec 26 '12 at 14:53
2

Top-level types, which are not nested into other types, can only have internal or public accessibility. The default accessibility for these types is internal.

From the MSDN link

1 Does the internal class remains internal to library ? or does becomes internal to the application is been merged with?

It remains internal to the application.

2 Are there chances that library will malfunction?

Technically it is possible. Suppose class A is internal to App, and also to one of the lib (with same namespace). Before merging there will not be any issue. After merging it will become issue to resolve ambiguous reference.

How The application (SmartAssemply/ILMerger) handles these is another issue (that I am not aware of)? They may choose to provide error information, or may not. They may choose to convert or may not.

Wont it be better that when merging the assembly that are internal should be made private so that that cant be used by the application they are merged in?

As specified top level types cannot be private/protected.

Tilak
  • 30,108
  • 19
  • 83
  • 131
  • Inside the assembly, types are referenced by token, not by name. So there is no problem. (But using reflection to look types up by name may misbehave) – Ben Voigt Dec 26 '12 at 15:04
0

MSDN is quite clear on this:

Internal types or members are accessible only within files in the same assembly...

Since you merged them into a single assembly, the internal classes are accessible to all files within the merged assembly.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • well i know for sure that it can be used within same assembly, but merging them violate rule that assembly should not have to be accessed by them application its merged with – Parimal Raj Dec 26 '12 at 14:39
  • I'm not sure I understand what you mean? Do you have an authoritative reference to that rule? – Frank van Puffelen Dec 26 '12 at 15:36
0

ILMerge can break libraries, although this only happens if the library is poorly designed.

The breakage will have nothing to do with internal. But if reflection is used to test what assembly something is loaded from, or search for a type in a specific assembly, the results will change.

I would be much more worried about the effects of merging on Code Access Security attributes than on internal. But essentially, don't merge assemblies that need to run at different levels of trust.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720