Running code during Assembly.Load

B

Brian Richards

Is it possible to run code in an assembly when it's loaded? In my case I
have an assembly that requires that the users path be setup correctly or
GetTypes() will throw an exception. Looking for a work around to just
requiring the user to have the Path environment setup in the calling
application.

class MyAssemblyLoader
{
public Type[] GetTypes(String assemblyName)
{
Assembly assembly = Assembly.Load(assemblyName); //causes code in
assemblyName to run and set Environment path from Assembly.dll.config

return assembly.GetTypes();
}
}
 
B

Brian Richards

Reposting to VC newsgroup because of information I found.

Is it possible to run code in an assembly when it's loaded? In my case I
have an assembly that requires that the users path be setup correctly or
GetTypes() will throw an exception. Looking for a work around to just
requiring the user to have the Path environment setup in the calling
application.

class MyAssemblyLoader
{
public Type[] GetTypes(String assemblyName)
{
Assembly assembly = Assembly.Load(assemblyName); //causes code in
assemblyName to run and set Environment path from Assembly.dll.config

return assembly.GetTypes();
}
}


I read that 2.0 C++/CLI supports module initialization where you can run
managed code. I haven't been able to find any examples though. Can anyone
help?
 
N

Nicholas Paldino [.NET/C# MVP]

Brian,

Why do you have an assembly.dll.config? You should take your config
settings and place them in the config file for the application that is using
the assemblies. This is the recommended way of doing it, and the way the
config classes have been set up.

But in regards to your question, if you want code to run in an assembly
when loaded, you will have to execute the code manually.

My recommendation is to create an attribute which takes a type. The
assemblies would then apply the attribute on the assembly level, and you
would get the attribute in your calling code through reflection. Then, you
can get the type.

Now, you can either call a static method with a known signature on the
type, or (and I would do this), create an interface that the type will
implement, and then create an instance of that type.

Then, you execute the methods on the interface implementation, and you
are done.

You might want to keep a flag indicating that the code was run as well.

Hope this helps.
 
B

Brian Richards

I reposted my question to the VC group because I beleive what I want is a
module initializer. The reason for the assembly.dll.config is because my
application is plugin based and I want to keep the configuration settings
with the plugin w/o having to merge them with application.exe.config.

Thanks


Nicholas Paldino said:
Brian,

Why do you have an assembly.dll.config? You should take your config
settings and place them in the config file for the application that is using
the assemblies. This is the recommended way of doing it, and the way the
config classes have been set up.

But in regards to your question, if you want code to run in an assembly
when loaded, you will have to execute the code manually.

My recommendation is to create an attribute which takes a type. The
assemblies would then apply the attribute on the assembly level, and you
would get the attribute in your calling code through reflection. Then, you
can get the type.

Now, you can either call a static method with a known signature on the
type, or (and I would do this), create an interface that the type will
implement, and then create an instance of that type.

Then, you execute the methods on the interface implementation, and you
are done.

You might want to keep a flag indicating that the code was run as well.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian Richards said:
Is it possible to run code in an assembly when it's loaded? In my case I
have an assembly that requires that the users path be setup correctly or
GetTypes() will throw an exception. Looking for a work around to just
requiring the user to have the Path environment setup in the calling
application.

class MyAssemblyLoader
{
public Type[] GetTypes(String assemblyName)
{
Assembly assembly = Assembly.Load(assemblyName); //causes code in
assemblyName to run and set Environment path from Assembly.dll.config

return assembly.GetTypes();
}
}
 
B

Ben Voigt

Brian Richards said:
Reposting to VC newsgroup because of information I found.

Is it possible to run code in an assembly when it's loaded? In my case I
have an assembly that requires that the users path be setup correctly or
GetTypes() will throw an exception. Looking for a work around to just
requiring the user to have the Path environment setup in the calling
application.

class MyAssemblyLoader
{
public Type[] GetTypes(String assemblyName)
{
Assembly assembly = Assembly.Load(assemblyName); //causes code in
assemblyName to run and set Environment path from Assembly.dll.config

return assembly.GetTypes();
}
}


I read that 2.0 C++/CLI supports module initialization where you can run
managed code. I haven't been able to find any examples though. Can anyone
help?

With a mixed assembly, DllMain gets called during assembly load, however
unmanaged code only (loader lock).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top