I have an issue which I don't quite understand. I am learning the architecture of C# and I gave myself a small project to practice on. One of the things I wished to do is to create a function that receives (string action, double x, double y) and activate a Math (the built in class of C#) function which name is @action on @x and @y dynamically. I read posts about how to do this using Reflection but it simply fail. It seems to work for classes I build but not for Math. the compiler rejects any method I tried claiming that "Math is a type and cannot be used in that context" whenever I tried to do so. There are plenty of examples on calling functions dynamically using Reflection. Here are a couple:
Calling a function from a string in C#
how to dynamically call a function in c#
Now for my questions:
A. What does the compiler screaming about? What does this error mean?
B. Why doesn't it work for Math but does so for classes I develop?
C. How do I make it work?
D. Is there a way to do so without using Reflection?
PS: If my question isn't clear I can give you an equivilent in JavaScript. You could easily achieve what I want by simple writing the following line:
return Math[action](x,y);
Thanks for any help.
Aucun commentaire:
Enregistrer un commentaire