vendredi 29 novembre 2019

Is using C#'s Type (Reflection) bad practice?

Forgive me if my question is improper, first time posting my own. I am trying to be more proper with my code while also trying to get things I've never tried before to work~ Playing around with code seeing what I can and can't do. Even if it has no real point or purpose. Just using it as a way to entertain myself and learn as I go.

My current project is having one script add more lines of code to a different file. I thought about this when I realized that code is just text, and you can make code that adds text to a file. So I was thinking of code that would write new methods that can be called. For this to work, I'd have to be able to call a method by a string name however. So I researched if I can do something like that. And this is what I found:

Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(TheCommandString);
theMethod.Invoke(this, userParameters);

ottobar's response here

I know that certain langues are not SUPPOSED to do certain things. Like C# can use var or dynamic, but it is best to avoid them as much as possible.

My understanding is this is because C# (unlike things like Python) like to work with "knowns", and doesn't like "unknowns"

Too Long Didn't Read:

Is Type thisType = this.GetType(); something I should only use in very specific situations like var and dynamic?





Aucun commentaire:

Enregistrer un commentaire