Wednesday, July 18, 2007

Return of Friendship

moz-screenshot-11[2][1][1][1]In the good old days, friendship was a meaningful construct.

On lone evenings, I like to look back to the long gone era of C++, long before the decadence of virtual machines and the sloth of garbage collection. 

Back in those days I could declare someone as a friend and this meant trust. Absolute, boundless trust. Even though this intimate relationship was not necessarily bi-directional (I was not necessarily the friend of my friends...sigh!) 

I know, its hard to grasp the full meaning and consequences of friendship for the insubordinate youths of today's Java- and C#-Sharp generation (and even more for their self-indulgent Ruby- and Phyton-offspring).

For a long time I had looked at the world of today with great despair, but recently a spark of new hope has found its way into my heart: Friendship has returned with the .NET Framework 2.0 and seeks its way into the troubled lives of the young generations.

Of course today's new friendship is not the same as in the old days: The friendship between assemblies its much more coarse than the delicate friendship between classes used to be. But hey, it's a beginning, don't call me a grumpy old man!

Here we have a modern declaration of friendship:

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Test")]

namespace Friends {

    public class PublicFriend {
        internal string InternalMethod() {
            return "I am Internal!";
        }
    }
    
    internal class InternalFriend {
        public string PublicMethod() {
            return "I am Internal!";
        }
    }
}

And here we have an example of how to enjoy the pleasures of this friendship:

    [TestFixture]
    public class Test {
        [Test]
        public void instantiate_internal_friend_should_work() {
            InternalFriend myFriend = new InternalFriend();
            Assert.IsNotNull(myFriend);
            string test = myFriend.PublicMethod();
            Assert.IsTrue(test.Length > 0);
        }

        [Test]
        public void calling_internal_method_should_work() {
            PublicFriend myFriend = new PublicFriend();
            string test = myFriend.InternalMethod();
            Assert.IsTrue(test.Length > 0);
        }
    }

Rejoice and praise the lords high up in Redmond for the mercy they show for our blunted souls!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...