Static constructors

I have a class library assembly, and I would like some code to get executed whenever a referencing assembly uses it.

Since it has no entry point (being a library) I've been trying to force static constructors to execute, but the behavior is that they only do so before the first property, or method is touched, or instance created.

So... is there any way to force some code in an assembly to run when it is loaded into an app domain?

Thanks!

Andrew

[457 byte] By [ajp] at [2007-12-24]
# 1

Don’t know if this old topic mixed DDL loading issue help you a bit, it’s up to your decision.

Peca55 at 2007-8-31 > top of Msdn Tech,Visual C#,Visual C# Language...
# 2

Well, there's a snippet in there that alludes to my current solution: I'm touching the class with the static constructor by creating an instance. Therefore, I'm assured the static constructor gets run when I create that instance.

This is undesirable though, because the client assembly must know to create an instance to force that code to run, and this defeats the purpose. I want the component assembly to run some validation code before it allows itself to be consumed.

As an insight to what I'm trying to do:

I have some generated code (svcutil.exe proxies), and I want to add things (properties, ICloneable implementations, override object.Equals, etc.) to the the types it generates in partial classes. I have a utility method marked up with a ConditionalAttribute("DEBUG") that checks that the type passed in (the type I'm adding things to) derives from a base class that all my DataContract types derive from.

So if the component ran that validating code immediately when it was loaded, and threw an excpeption for debug builds, I could avoid arduous debugging when a namespace or type name changes, making the data contract type no longer implement object.Equals, or provide a certain property (which may only be used in data-binding so it's never strongly type checked ie. DataMember="PropertyNameIAdded")

Any insight to a better solution is very welcome!

Thanks!

ajp at 2007-8-31 > top of Msdn Tech,Visual C#,Visual C# Language...