InfoQ

News

Friend Assemblies and Unit Testing

Posted by Jonathan Allen on Jun 04, 2007 06:05 AM

Community
.NET
Topics
Unit Testing
Tags
C# ,
Visual Basic.NET
One of the major problems with unit testing is the need to access non-public classes and methods. There are numerous ways to work around this such as using proxy classes and reflection, but wouldn't it be nicer if an assembly could just treat non-public fields as if they were public?

C# 2.0 has this ability via friend assemblies. Normally types marked as internal as only accessible from within the assembly that defines it. However, when an assembly says that another assembly is a friend via the InternalsVisibleTo attribute, that second assembly can access any type in the first one that is marked internal.

According to Tim Ng, the ability to define friend assemblies will be added to the next version of Visual Basic.
strongly signed assemblies only? by andrew mcveigh Posted Jun 4, 2007 6:35 AM
Re: strongly signed assemblies only? - No, that is a documentation error. by Jonathan Allen Posted Jun 4, 2007 10:58 AM
  1. Back to top

    strongly signed assemblies only?

    Jun 4, 2007 6:35 AM by andrew mcveigh

    I may be wrong, but I think this only works for strongly signed assemblies, which takes away some of the usefulness. at least it used to be that way in C#2.0 ... Andrew

  2. The MSDN documentation is highly inaccurate for this feature. The first error is the implication that the assemblies must be signed. Via testing I just conducted, I found that you do not need signed assemblies to use this feature. According to Tim Ng in the linked article, if friend assembly only has to be strongly signed if the assembly being shared is signed.

    One of the rules regarding friend assemblies is that if the declaring assembly is a strong name assembly (that is, that it specifies a key file/container), then you can only declare friends that are also strong name assemblies. This means that you must specify the entire 128 bit public key in the InternalsVisibleTo attribute: InternalsVisibleTo("A, PublicKey=<128-bit Key>").
    The second error I found in the documentation is that it implies your friend assembly has access to "all non-public types". In reality, it only has access to types marked as "internal". Private types and methods remain so.

Educational Content

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.