locked
call a method using a string without reflection RRS feed

  • Question

  • Hello All,

    I'm trying to create a method that gets a string as argument and calls a function with the same name.

    However, I'm not allowed to use reflection at all.

    Is there any other way to do this?

    Barak

    Monday, August 2, 2010 10:53 AM

Answers

  • You must be using reflection wrong.  It cannot get "ripped out" when creating a CAB - it's part of the framework itself.  Show us your failing reflection code.
    • Marked as answer by warrentang Friday, August 6, 2010 5:57 AM
    Monday, August 2, 2010 5:04 PM

All replies

  • Can you elaborate a bit more?

    Monday, August 2, 2010 11:09 AM
  • Sure. :)

    Basically, what I need is something like that:

    I need to create a method (let's say it's called RunMethods).

    If I call RunMethods with a string argument "foo" the method will invoke another method called foo()

    (we'll assume that to every "foo" that I send as argument, I have a matching foo() method)

    I hope that now things are clearer.

    If not, let me know,

    Barak

     

     

     

     

     

     

    Monday, August 2, 2010 11:28 AM
  • I need to make this clear:

    I cannot use reflection. I know that reflection has a great way of solving it - but I need another way to solve this.

    thanks,

    Barak

    Monday, August 2, 2010 11:32 AM
  • I would say that reflection is the only of accomplishing your task
    Monday, August 2, 2010 11:38 AM
  • use if statement / switch to check the string, and accordingly call the method it requires.

    -or-

    convince the people that forbid you to use reflection that it is required

    Monday, August 2, 2010 12:12 PM
  • is there someting in pinvoke I can use? or any other way?

    Monday, August 2, 2010 12:18 PM
  • No. Reflection would be the only proper way of accomplishing your required task.

    You can of course create a huge switch/case check for every possible string and call the method from it. But reflection would really be the right way to go

    Monday, August 2, 2010 12:24 PM
  • Why is it that you are trying to avoid reflection?
    Joel Ivory Johnson | http://www.j2i.net | Device Application Development MVP
    It takes all the running you can do to stay in one place.If you want to get somewhere else,you must try to run at least twice as fast as that.
    Monday, August 2, 2010 12:42 PM
  • in order to avoid refactoring. This is already a part of the program now, so there's nothing I can do to change it. using reflection will result in nullException, as the reflection is totally ripped out when creating a cab.

    any idea for alternative?

    Monday, August 2, 2010 1:28 PM
  • You must be using reflection wrong.  It cannot get "ripped out" when creating a CAB - it's part of the framework itself.  Show us your failing reflection code.
    • Marked as answer by warrentang Friday, August 6, 2010 5:57 AM
    Monday, August 2, 2010 5:04 PM
  • Have you solved the problem? If not could you tell us the environment (the version of .NET CF, Windows Mobile, etc.)? Could you show us the source code, and the details of the exception (error message, exception type, the stack trace)? You must have done something wrong, probably passing the wrong arguments.
    Please mark the post that helps you, and unmark that does not. This benefits our community.
    Wednesday, August 4, 2010 6:22 AM