InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Friend Assemblies and Unit Testing

Posted by Jonathan Allen on Jun 04, 2007

Sections
Development
Topics
.NET ,
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
Re: strongly signed assemblies only? - No, that is a documentation error. by Jonathan Allen Posted
  1. Back to top

    strongly signed assemblies only?

    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. Back to top

    Re: strongly signed assemblies only? - No, that is a documentation error.

    by Jonathan Allen

    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

Jesper Boeg on Priming Kanban

In this interview, Jesper Boeg, author of the new InfoQ book – Priming Kanban, discusses the keys to using Kanban effectively, and how to get started if you are currently using other approaches.

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.