473,387 Members | 1,812 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.

Reflection

Hi All,

I am trying to use reflection for getting all the classes from the dlls of a
software written by my company. I am using this line:

Assembly.LoadFile(s).GetTypes()

Gettypes fails, I guess because when one dll refers to another one of mine,
Reflection does not find that dll. This is what the exception contains
anyway. So I realized that I'd need to copy the dlls, or register them to
the GAC. And then I tried Reflector, and to my amazement, everything was
working fine! Then I tried some tricks, invoked Assembly.LoadFile() for all
the referenced dlls before trying GetTypes() on the one in question, still
no success. Does anyone have an idea how it can be done without copying and
GAC?

I appreciate all your answers.

Thanks,

Jozsi


Oct 26 '05 #1
3 2000
Try this before you load and see if it helps.
String path = Path.GetDirectoryName(Path.GetFullPath(assemblyNam e));
#if VS2003
AppDomain.CurrentDomain.AppendPrivatePath(path);
#else
AppDomain.CurrentDomain.SetupInformation.PrivateBi nPath = path;
#endif

Hope it helps
Leon Lambert

Jozsef Bekes wrote:
Hi All,

I am trying to use reflection for getting all the classes from the dlls of a
software written by my company. I am using this line:

Assembly.LoadFile(s).GetTypes()

Gettypes fails, I guess because when one dll refers to another one of mine,
Reflection does not find that dll. This is what the exception contains
anyway. So I realized that I'd need to copy the dlls, or register them to
the GAC. And then I tried Reflector, and to my amazement, everything was
working fine! Then I tried some tricks, invoked Assembly.LoadFile() for all
the referenced dlls before trying GetTypes() on the one in question, still
no success. Does anyone have an idea how it can be done without copying and
GAC?

I appreciate all your answers.

Thanks,

Jozsi

Oct 26 '05 #2
Hi Leon,

thank you for the suggestion. I could not manage to make it work even with
the lines you suggested, but this might also be needed for making the stuff
run.

Thank you,
Jozsi

"Leon Lambert" <la******@inil.com> wrote in message
news:Or**************@TK2MSFTNGP09.phx.gbl...
Try this before you load and see if it helps.
String path = Path.GetDirectoryName(Path.GetFullPath(assemblyNam e));
#if VS2003
AppDomain.CurrentDomain.AppendPrivatePath(path);
#else
AppDomain.CurrentDomain.SetupInformation.PrivateBi nPath = path;
#endif

Hope it helps
Leon Lambert

Jozsef Bekes wrote:
Hi All,

I am trying to use reflection for getting all the classes from the dlls
of a software written by my company. I am using this line:

Assembly.LoadFile(s).GetTypes()

Gettypes fails, I guess because when one dll refers to another one of
mine, Reflection does not find that dll. This is what the exception
contains anyway. So I realized that I'd need to copy the dlls, or
register them to the GAC. And then I tried Reflector, and to my
amazement, everything was working fine! Then I tried some tricks, invoked
Assembly.LoadFile() for all the referenced dlls before trying GetTypes()
on the one in question, still no success. Does anyone have an idea how it
can be done without copying and GAC?

I appreciate all your answers.

Thanks,

Jozsi


Oct 26 '05 #3
Was hoping to avoid this but here is a more complex way to try.
First define a ResolveEventHandler in the class where you are doing the
loading. It would look something like the following. Sorry for ugly
look. This UseNet Reader is wrapping stuff

private ResolveEventHandler resolveEventHandler;

Then somewhere before you try to load add a handler. Something like this.

resolveEventHandler = new ResolveEventHandler(AssemblyResolver);
Thread.GetDomain().AssemblyResolve += resolveEventHandler;

Then create a resolver method something like this.
In my case i had a command line argument to specify
additional directories where to search for support
dlls. So the handler will also search there.
public Assembly AssemblyResolver(Object sender,ResolveEventArgs args)
{
String path;
// try looking in same directory as the original one first

// assembly is a class variable containing the starting loaded
// assembly

// assemblyNameToResolve is a class string variable.

path=assembly.Location.Substring(0,assembly.Locati on.LastIndexOf("\\") + 1);
assemblyNameToResolve = args.Name;
if ((assemblyNameToResolve.IndexOf(",") != -1) &&
(assemblyNameToResolve.IndexOf("Culture") != -1))
assemblyNameToResolve=assemblyNameToResolve.Substr ing(0,assemblyNameToResolve.IndexOf(","));

// I had a collection of loaded assemblies.
// This check to see if i already loaded it and just
// return it if it was already loaded
Assembly assm = assembliesLookup[assemblyNameToResolve] as Assembly;
if (assm != null)
return(assm);
try
{
assm =
Assembly.LoadFrom(String.Concat(path,assemblyNameT oResolve,".dll"));
}
catch (Exception)
{
}
// if not found try looking in directories passed in
// as command line arguments
if (assm == null)
{
foreach (String assemblyPath in extAssemblyDirectory)
{
try
{
assm =
Assembly.LoadFrom(String.Concat(assemblyPath,assem blyNameToResolve,".dll"));
if (assm != null)
break;
}
catch (Exception)
{
}
}
}
if (assm == null)
throw(new Exception("Cant find assembly " +
assemblyNameToResolve));

// add to collection of already looked up assemblies
assembliesLookup.Add(assemblyNameToResolve,assm);
return(assm);
}

When done using the loading class add the following code to remove the
handler.

Thread.GetDomain().AssemblyResolve -= resolveEventHandler;

This should work because it did for me. Sorry for the ugliness because
of line wrap
Leon Lambert

Jozsef Bekes wrote:
Hi Leon,

thank you for the suggestion. I could not manage to make it work even with
the lines you suggested, but this might also be needed for making the stuff
run.

Thank you,
Jozsi

"Leon Lambert" <la******@inil.com> wrote in message
news:Or**************@TK2MSFTNGP09.phx.gbl...
Try this before you load and see if it helps.
String path = Path.GetDirectoryName(Path.GetFullPath(assemblyNam e));
#if VS2003
AppDomain.CurrentDomain.AppendPrivatePath(path);
#else
AppDomain.CurrentDomain.SetupInformation.PrivateBi nPath = path;
#endif

Hope it helps
Leon Lambert

Jozsef Bekes wrote:
Hi All,

I am trying to use reflection for getting all the classes from the dlls
of a software written by my company. I am using this line:

Assembly.LoadFile(s).GetTypes()

Gettypes fails, I guess because when one dll refers to another one of
mine, Reflection does not find that dll. This is what the exception
contains anyway. So I realized that I'd need to copy the dlls, or
register them to the GAC. And then I tried Reflector, and to my
amazement, everything was working fine! Then I tried some tricks, invoked
Assembly.LoadFile() for all the referenced dlls before trying GetTypes()
on the one in question, still no success. Does anyone have an idea how it
can be done without copying and GAC?

I appreciate all your answers.

Thanks,

Jozsi


Oct 27 '05 #4

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

Similar topics

0
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
2
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
2
by: Mark | last post by:
Am I out of my mind if I use Reflection everytime someone logs into our site to get and track the current Major/Minor/Build/Revision version that the person is viewing our site through? This...
0
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to...
3
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
9
by: Kuberan Naganathan | last post by:
Hello all Does anyone know of a good way to use reflection in c++? I don't mean simply using rtti or dynamic casting. I'm talking about java/c# style reflection where an actual instance of...
17
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
0
by: Gustavo Arriola | last post by:
Hola a todos! Estoy intentando ejecutar un método usando Reflection. El código es el siguiente: public static void SoapHandler(Exception Error) { try { Type assemblyType;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.