473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Running code during Assembly.Load

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();
}
}
May 24 '06 #1
4 8212
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?
May 24 '06 #2
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]
- mv*@spam.guard.caspershouse.com

"Brian Richards" <br*******@gmail.com> wrote in message
news:eY**************@TK2MSFTNGP04.phx.gbl...
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();
}
}

May 24 '06 #3
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uM**************@TK2MSFTNGP03.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Brian Richards" <br*******@gmail.com> wrote in message
news:eY**************@TK2MSFTNGP04.phx.gbl...
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();
}
}


May 24 '06 #4

"Brian Richards" <br*******@gmail.com> wrote in message
news:OP**************@TK2MSFTNGP03.phx.gbl...
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).

May 30 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Clinton Pierce | last post by:
I need to be able to test at runtime whether or not a component is going to load properly from the Intranet. Is there a way to query, "Will the current running assembly trust a component loaded...
2
by: Mark | last post by:
I got this code from the Internet. Demo on the site works fine but downloaded version is not. I am a very new to .NET, I cannot figure this out, please help. Here is the code: ASPX code: <%@...
3
by: ctk70 | last post by:
I'm trying to run a ASP.NET 1.1 app on a Windows 2003 server. The app runs fine on my local workstation (Windows 2000 SP4). Both the server and workstation have Visual Studio .NET 2003 installed. ...
12
by: Karina | last post by:
Hello: I am taking over someone else's code and my users are experiencing tons of problems but when I try to go into the source code to edit and try to fix these problems, I realize that all the...
9
by: Michael.Suarez | last post by:
Suppose I have a program that prompts you with a dialogbox to enter a password. If you get the password correct, it allows you into the program, else it kills the program. Suppose that when I...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
6
by: Andrus | last post by:
VCSE 2005 .NET 2 WinForms I created assembly at runtime and added mydll.dll reference to it. mydll.dll is in applicatino startup directory. When I change mydl.dll to never version, this assembly...
0
by: Gtu108 | last post by:
Hi, I am getting an error while uploading a published version of site. The error is like : Description: An error occurred during the parsing of a resource required to service this request....
0
by: creo | last post by:
OS: Windows XP (32 bit) NET: .Net 2.0 runtime DevEnv: VS 2008 C++ and C# I am attempting to use an unmanaged C++ library to load a managed C# assembly and hand off control. I have the following...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.