CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jul 2004
    Posts
    37

    Reflection and arrays

    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
    Last edited by SSix2; October 23rd, 2008 at 11:05 AM.

  2. #2
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: Reflection and arrays

    Make the definition of A public in 3rd party dll.
    The difficulty is that you have no idea how difficult it is.

    .Net 3.5/VS 2008

  3. #3
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Reflection and arrays

    maybe something like this...

    Code:
    A[] 3rdPartyArray = new A[];
    
    SomeFunction(ref 3rdPartyArray);

  4. #4
    Join Date
    Jul 2004
    Posts
    37

    Re: Reflection and arrays

    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.

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Reflection and arrays

    Quote Originally Posted by SSix2
    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????
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Reflection and arrays

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Reflection and arrays

    Quote Originally Posted by SSix2
    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.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Reflection and arrays

    Quote Originally Posted by JonnyPoet
    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).
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Jul 2004
    Posts
    37

    Re: Reflection and arrays

    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.

  10. #10
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Reflection and arrays

    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.....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  11. #11
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Reflection and arrays

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  12. #12
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Reflection and arrays

    Quote Originally Posted by darwen
    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>...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  13. #13
    Join Date
    Jul 2004
    Posts
    37

    Re: Reflection and arrays

    Quote Originally Posted by TheCPUWizard
    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.

    Quote Originally Posted by TheCPUWizard
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured