0

So I'm relatively new to c#, and I've looked around but couldn't find anything close to what I'm looking for.

I want to call a method(s) with a string, but not a specific one.

For instance, assume we have two methods: one named Cat and one named Dog.

Suppose I have string Input = "Dog"; How can I do something like Method.Input(); to call the method with the name corresponding to information... if that makes sense.

hades _
  • 3
  • 3
  • 1
    I think there is similar question in here: https://stackoverflow.com/questions/29034093/create-instance-of-class-and-call-method-from-string/29034215 – gguney Feb 19 '22 at 04:33
  • 1
    This one will probably be pretty close to what you are looking for too: https://stackoverflow.com/questions/540066/calling-a-function-from-a-string-in-c-sharp General be careful whenever you use Reflection or user executable code as they can pose significant security risks – Steve Byrne Feb 19 '22 at 04:34
  • @SteveByrne do you think you could explain to me how exactly that works though? – hades _ Feb 19 '22 at 05:06
  • Eval & reflection executes things at run time (when the program runs) as opposed to compiled (when you build the program into an exe). In and of itself Eval and reflections work fine, but when users are allowed to manipulate them (for instance by allowing a user to type the name of a function) they can access parts of your program they shouldn't be able to, or other sections of memory your program has access to. The whole subject is a little in-depth, beyond what we can do in comments but we could take it to chat most likely – Steve Byrne Feb 19 '22 at 06:25
  • @SteveByrne i see. Is there any other way we can be in contact so you could help me understand the concept of how to do it as well if you'd like? – hades _ Feb 19 '22 at 21:26

0 Answers0