Поделиться через


Here I come...

Hi Friends,
This is Jaiprakash, working as SDET in Visual J# team for the last few years. During this stint with J#  I have learnt quite a lot interesting things about different aspects of product development, about .net, about J# and had some real cool experiences. Through this channel I am going to share them with you all.

I would love to start with something which really confused me when I started working with J# team. In J# access specifier "protected" has special behavior from other .net languages e.g. C#.
If you write a protected inner class in J#, compile it and refer the assembly in a C# project then your J# class is shown public not protected. Confusing!!!

So here is the explanation for this special behavior...

In J# following are the mappings of Access Specifiers to MSIL (Microsoft Intermediate Language - The intermediate language to which your code is compiled in .net) semantics…

Private in J#  ->  Private in IL
Public in J#  ->  Public in IL.
Package in J#    ->   Nothing in IL
Protected in J#  ->  Public in IL (By default)

The reason why J# protected doesn’t map to protected in IL is that each access specifier is superset of the other and ‘protected’ is superset of ‘package’ in J#. In J# package specifier means that access is available to classes in the same package and the package can be distributed in more than one assembly. Since ‘Protected’ in J# implies Package also, therefore in addition to obeying the general semantics it must confirm to Package semantics too. Since Package can be distributed across different assemblies, we simulate the required behavior by mapping ‘protected’ in the source code to ‘public’ in IL.

This is the default behavior in J#. If you want to turn off this behavior then you must compile your source code with securescoping compiler switch (/securescoping[+|-]). This option marks "package" scope as "internal" in meta data.

If you are compiling in Visual Studio then you can turn on securescoping option from Project Properties pages. Following are the steps to follow...
1. Right click on Project node in solution explorer.
2. Click project properties.
3. Click Build Tab in Project properties designer.
4. Click Advanced Button.
5. Check Secure Scoping check box.
6. Build your project.

The default behavior is there for the compatibility with Java language and secure scoping option is there to allow user to have consistency with other languages supported by CLR.

Hmmm…it's getting too lengthy so let’s have a break. Keep visiting as I would be reveling more such confusing but interesting :) stories here.