6

I'd like to automagically execute some code before certain class constructor gets executed (to load some externall assmebly that class requires), all in C#, .NET 2.0

EDIT:

public class MyClass 
{
    ThisTypeFromExternalAssembly variable;
}

And what I really need is to have assembly loader that is 'attached' somehow to MyClass, to load externall assembly when it is needed. This must happen before constructor, but I would not like to need to call some Init() before constructing MyClass() object

3
  • 1
    Can't you do that (load ext libraries) directly in the class constructor? Commented Mar 1, 2011 at 13:14
  • I can't: class can not be constructed without externall assembly, And I would not like to do adtional init before using this class. Commented Mar 1, 2011 at 14:02
  • 2
    The AssemblyResolve event already does this. You just need to make sure that you subscribe to the event before the JIT compiler needs the type. In other words, you cannot refer to any of the assembly's types in your startup method. If subscribing the event is the "Init" method you want to avoid then, no, you cannot do without that. These are otherwise rather silly gyrations just to avoid copying two files instead of one. Commented Mar 1, 2011 at 14:33

4 Answers 4

6

You can use the static initialiser for the class:

static ClassName( )
{

}

This will be called before any instances of ClassName are constructed.

Given the update you would do:

public class MyClass
{
    ThisTypeFromExternalAssembly variable;

    static MyClass( )
    {
        InitialiseExternalLibrary( );
    }

    public MyClass( )
    {
         variable = new ThisTypeFromExternalAssembly( );
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

@Nick as far as know static contructor is called once ( when type system needs to create first time from this object and which is unpredictable). so if you need to execute some code after every instantination this probably wont work.
@adt, I guess the O.P.'s question is a bit ambiguous. Does he want the code to be called every time before a constructor gets called? Or just the first time before any instances of the class are constructed?
Tried that, didn't work. I have exception becouse of unreolved class dependency.
Hey Adam, could you post your updated code and where the exception is getting thrown?
And Adam you want to interfere everytime class instantinated or just once?
|
4

Could you use a static constructor for this?

class SimpleClass
{
    // Static constructor
    static SimpleClass()
    {
        //...
    }
}

From the MSDN article:

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

2 Comments

@Alex: That's just how I roll :P
OP already mentionned a "class constructor"... Class constructor, AFAIK, is a synonym of "static constructor", as opposed to "instance constructor". The question, as I see it, is why he can't use it directly.
3

If it's to load an assembly, that sounds like you just want to do it once, in which case a static constructor may be appropriate:

public class Foo
{
    static Foo()
    {
        // Load assembly here
    }
}

Note that if this fails (throws an exception), the type will be unusable in that AppDomain.

Is there any reason why you're not just using normal type resolution to load the assembly though? Wouldn't the assembly be loaded automatically when you need to use part of it? Could you give more details about the problem you're trying to solve?

1 Comment

I want to include dll in exe using following method blogs.msdn.com/b/microsoft_press/archive/2010/02/03/… Baseline is this: I have a framework that depends on the 'session' object (you use the whole framework by this object), but this object alreade depends on code in dll i want to include. I'd prefer not to add any special init for the framework, thats why I'd like to execute code before the session object is constructed.
0

you might want to use an aop framework like postsharp, which allows to interfere function call by using attributes .

http://www.sharpcrafters.com/solutions/monitoring#tracing

Postsharp: how does it work?

http://www.codeproject.com/KB/cs/ps-custom-attributes-1.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.