Archive for the ‘tips’ Tag

How to Access Nonpublic Types of a Referenced Assembly

Most of the solutions I work with consist of at least 3 projects: data access layer, business logic layer, windows app, etc. Each of those projects contains at least a few instances of code that throws exceptions. Typically, these exceptions are used to indicate invalid parameters or internal errors that cannot be handled locally. I tend to use resource files to store assembly messages because it allows me to enforce a common exception message format.

In small and medium-sized solutions, the number of exception messages in the resource file is small: (15-20) and they are not very likely to be reused. Large solutions (5+ projects), on the other hand, often contain 50+ messages and messages are likely to be reused. The logical thing to do, in this case, is to put all exception messages in a separate assembly (ProjectName.FeatureName.Resources) and reference it.

This sounds like an easy thing to do and it is. Except for one minor issue… The designer.cs file generated by the Visual Studio resource editor is marked internal and so are it’s members. It is possible to edit the file manually to change resource visibility to public, but these changes are lost the moment you edit the file using the designer because it re-generates the designer.cs file.

There is a little known attribute in the .NET Framework that helped me get around this problem: InternalsVisibleTo.

This attribute makes all nonpublic types in an assembly it is applied to visible to another assembly.

Example: AssemblyInfo.cs

[assembly:InternalsVisibleTo(“ProjectName.FeatureName.Data“)]

[assembly:InternalsVisibleTo(“ProjectName.FeatureName.Services, PublicKey=2e049ab9234d98234“)]

The first line specifies that all nonpublic types are visible to assembly ProjectName.FeatureName.Data (version and culture of the assembly are not taken into account).

The second line specifies that all nonpublic types are visible to assembly ProjectName.FeatureName.Services that has public key 2e049ab9234d98234.

I Need Sleep…

It’s 00:11 GMT. I’ve just finished building the new PC for my parents and am currently trying to get through the rss posts that have piled up over the last few days. 187 posts to go (of 598 discovered this morning). Not bad.

After going through the first 200 posts, I though it would be a good idea to review my blog reading process. You’ve probably seen something similar on other blogs, but I’ve put together a list of suggestions that could be useful should you find yourself in a similar situation:

  1. If you have 598 posts to go through, consider just deleting the whole lot – the time spent sifting through all that information may or may not be worth the result.
  2. If find yourself deleting >90% of posts in a particular feed – just remove that feed. Or, even better, try to find a similar feed that focuses on a subset of information that the original feed provided.
  3. Remove feeds that have lost their quality and/or integrity. View an earlier post of mine for an example.
  4. Don’t read a long post immediately . Eyeball the post. If you think it’s good – mark it ‘To Read‘ and read it some other time; otherwise delete it.

That’s all folks. So until next time – Good fight, good night.