Hi to all!
I'm migrating from Delphi to C# and I'm stuck at this one.
In Delphi, each unit had initialization and finalization sections. In those section, I could put code that was executed on unit loading (before any form was created). Finalization was executing when unit was unloading. I used that feature to build very modular application. I had dozen's of DLL's that were dynamically loaded as needed, and main form of application was building menu according to forms in loaded DLL's (because each unit called my registerForm function, so I exactly new which forms were actually present - notice that none of these forms were created, they were just registered in initialization section of unit in which they were declared. Each form was created when user clicked it in menu).
I've noticed that in C# everything must be in a class. Is there any mechanism that could substitute this great feature from Delphi
Something like classes with self-executing contructors :).
Thanks,
Mario B.

Initialization...finalization
Dave Balsillie
After reading the article, I have a question (which I'm sure I could find answer in other places, but let's conclude this one). What is a module in C# In VB, I saw there is keyword "module", but there is nothing like that in C# and in that article, modules play a big part.
So, what is a module in C# How it is defined
witkamp
In C# the two major areas that would help you with this are Constructors and Finalizers (called Destructors in C++). Consider the following class:
public class MyClass
{
public MyClass()
{
//Constructor
}
~MyClass()
{
//Finalizer
}
}
When a new instance of MyClass is created the constructor is called and it is there that you can put your initial configuration for the class.
Later when the garbage collector has begun work on your class it will call the finalizer to give it one last opportunity to clean up any resources it was working with before the GC does it for it.
Because the timeliness of the GC in calling the finalizer is not guaranteed it is often a good practice to throw in a Dispose method that can be called by you or the system to do any clean up that may be desired before the GC gets its chance.
Take a look at this blog post for a slightly more in depth example.
eddy1421
Brendan, you missed the point.
For code inside constructor to be executed you must explicitly call that constructor from somwhere in your code. That doesn't do the job for me.
Initialization section of unit in Delphi was self-executed on module load.
Here is example. Imagine you have 150 forms located in about 30 assemblies. Those assemblies are part of one big product. Let's say production managment software. So, modules would be: planning.dll, preparing.dll, managing.dll, customers.dll, workers.dll, warehouse.dll, sales.dll, development.dll and so on, and so on.
There is also one more assemblie: main.dll. This assemblie contains fundamental user interface in which all other forms from other assemblies are loaded. So, everything is dynamic. We could say it is MDI type of application.
Main.dll also builds menu dynamically only for forms that are loaded with other assemblies. So, if you load no assemblies, menu will be empty. If you load them all, menu will have 150 items.
Why this One customer might want planning and preparing.dll, but not others. The other one might want warehouse.dll and sales.dll, but not others.
With a system like this, I don't have to change anything in order to add or remove a module. I simply remove it from parameters (ini file or registry, no matter) which define which modules are installed (and will be loaded).
I can't do this in constructors, since to accomplish that, I would have to call all the constructors in all the forms somehow and this is definetely not an option.
I need a routine that will be self-executed when loading assemblie, so all the forms inside assemblie would register itself to main.dll so main.dll can be aware of their existence and can put them in the menu.
Regards,
Mario B.
gaozhi
Mario,
Coming from a Delphi background myself, I understand the nature of your problem...having been there myself. The initialization section is indeed useful for the situation you describe.
You might get some pointers from this article:
http://www.codeproject.com/csharp/suiteapp.asp
It demonstrates dynamic loading and querying of assemblies, it might prove useful in your scenario: i.e. run-time discovery.
And of course, you can't use static constructors either as they are only guaranteed to be called prior to an instance being touched...which is little use to you.
If I discover anything better in my travels, I'll post back here.
HTH
Yves1
They can be created using csc /target:module file.cs (via the command-line). Modules can be useful if you need to mix .net languages as each module can be written using other .net languages.
/target creates an output file with the extension .netmodule, which is then picked up via csc /addmodule:file.netmodule ...
More information can be found here:
http://blogs.msdn.com/junfeng/archive/2005/02/12/371683.aspx
http://abstractvb.com/code.asp A=1055
tha0