Click to See Complete Forum and Search --> : Reflection and arrays


SSix2
October 23rd, 2008, 11:03 AM
I am trying to call a method in a 3rd party dll via reflection. Their method looks something like this:

void SomeFunction(ref A[] b)

I can handle the ref portion but I dont know what to do concerning the A array. I was thinking maybe I could use List<A> and then call toArray. The problem is A is defined in the 3rd party dll too and has to be gotten via reflection. I dont know how to create it. Any ideas?

Thanks in advance.

2.0

jasonli
October 23rd, 2008, 11:46 AM
Make the definition of A public in 3rd party dll.

eclipsed4utoo
October 23rd, 2008, 11:58 AM
maybe something like this...


A[] 3rdPartyArray = new A[];

SomeFunction(ref 3rdPartyArray);

SSix2
October 23rd, 2008, 01:31 PM
Thanks for your answers but its not that simple.

The class is public and declaring
A[] 3rdPartyArray = new A[];

would work fine if I didnt need to use reflection.

Unfortunately, I have to use reflection and therefore need to do something like this:

List<A> myList = new List<A>();

But I have to retrieve A via reflection...because I dont want to reference the assembly in my project file.

TheCPUWizard
October 23rd, 2008, 02:12 PM
Thanks for your answers but its not that simple.

The class is public and declaring
A[] 3rdPartyArray = new A[];

would work fine if I didnt need to use reflection.

Unfortunately, I have to use reflection and therefore need to do something like this:

List<A> myList = new List<A>();

But I have to retrieve A via reflection...because I dont want to reference the assembly in my project file.

You can not mix "have to" (absolute requirement/no other options) and "want to" (a desire) ....WHICH is it????

darwen
October 23rd, 2008, 02:14 PM
You should use object[] or ArrayList (the .NET 1.1 version of List which works on System.Object).

If you have no reference to type A that's all you can do.

You can then invoke methods on the instances of the object (items in the array) using reflection.

Darwen.

JonnyPoet
October 23rd, 2008, 02:15 PM
But I have to retrieve A via reflection...because I dont want to reference the assembly in my project file.This seems me to be a bit strange.
If you have the rights to use the 3rd party dll why then not simple reference to it. if you want to use it, you will have to reference to it.

There is a tool in the web names something like LutzRoedersReflector which can be used to analyse dlls. So even if I cannot get your intentions background this will maybe answer your question.

TheCPUWizard
October 23rd, 2008, 02:28 PM
Lutz Roeder's Reflector.

Actually Lutz has turned that project over to RedGate and moved on to "bigger and better" things....(This tool should be part of EVERY .Nertdevelopers environment).

SSix2
October 23rd, 2008, 02:56 PM
TheCPUWizard: I need to not reference the assembly in the project. Therefore, I need to use reflection inorder to obtain A's type and create a array. Maybe not but I assumed reflection would be the best mechanism to help in the matter.

darwen: I was hoping these werent the only options. Nutz...

JonnyPoet: Certain enviroments that I work in require somethings not be distributed. Currently I am doing the obvious (just referencing the assembly in the project) and everything works fine but it causes some merging and maintenance issues later in the process.

eh. I thought I would just ask to see if someone had a neat idea.

TheCPUWizard
October 23rd, 2008, 03:55 PM
To utilize the class, you have to load it. Period.

This is independant of referencing it at the project level (giving you compile time access) or of dynamically loading it.

So since your code will need access to the DLL at runtime, I fail to seew why you can not reference it as part of the project.

This has NOTHING to do with distributing the DLL.....

darwen
October 23rd, 2008, 04:28 PM
I thought he might be writing some form of plug-in system.

But in a plug-in system you usually have interfaces which are in a referenced assembly and included in the project. These are then implemented by the objects in the dynamically loaded assembly to avoid calling the objects in question by reflection.

Darwen.

TheCPUWizard
October 23rd, 2008, 08:41 PM
I thought he might be writing some form of plug-in system.

But in a plug-in system you usually have interfaces which are in a referenced assembly and included in the project. These are then implemented by the objects in the dynamically loaded assembly to avoid calling the objects in question by reflection.

Darwen.

Exactly...in which case all programmatic interaction would be with members of the interface. So you might have a List<Interface> but you would not have a List<Class>...

SSix2
October 27th, 2008, 05:28 PM
To utilize the class, you have to load it. Period.

This is independant of referencing it at the project level (giving you compile time access) or of dynamically loading it.

So since your code will need access to the DLL at runtime, I fail to seew why you can not reference it as part of the project.


The DLL (actually DLLs) can not be used in all environments that I work in. The project file must be updated in other departments and causes time/effort.


This has NOTHING to do with distributing the DLL.....

I was not referring to distribution to the customer.



I was asking a question concerning arrays and reflection. I guess it cant be done.