Interesting post by Chris Daly on the advanced-dotnet list today. He questions the C# language spec, which says that the following is illegal: public class A { protected int x; } public class B: A { static void F(A a, B b) { a.x = 1; // Error, must access through instance of B b.x = 1; // Ok } }
The equivalent Java is legal. When I statically compile the Java equivalent of A and B into two different assemblies, to resulting B.dll is unverifiable. It's not just a C# language issue, the CLR restricts access to protected members in this way. When both types are compiled into the same assembly there is no problem, because protected is compiled as famorassem so any type in the assembly already has access (this is needed because protected also implies package access, which has no CLR equivalent).
I wonder if a work around is required.
UPDATE: I wasn't quite awake yet. Of course, the reason that the above code works in Java is because protected implies package access, it has nothing to do with the protectness of the field. When you move A and B into different packages, javac fails with the same error as the C# compiler.
2:32:21 PM Comments
|