0

edit Hmm, I don't believe this is a duplicate of other questions asking about var. I am asking about Reflection, and just curious if it should be used sparingly like var

Also an extra shout out to Flydog57 for their responses in comments, I appreciate them~ edit

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?

Community
  • 1
  • 1
  • 3
    There is no reason to avoid `var`; it's a very useful keyword. – Herohtar Nov 30 '19 at 06:03
  • There are situations where reflection is necessary. Mapping libraries such as AutoMapper use reflection. So do dependency injection libraries and ORMs. – Matt U Nov 30 '19 at 06:09
  • please check following https://stackoverflow.com/q/4868477/6923146 – Hardik Masalawala Nov 30 '19 at 06:17
  • Not entirely, I know when to use var, but I also know that using it carelessly is not proper or recommend. I'm asking if Type should be used the same way, carefully – NeoChrisOmega Nov 30 '19 at 06:21
  • There is no cost to using `var`. Whenever it is used, the type must be fully known to the compiler. The only cost is readability in some cases. Reflection does have some costs, particularly when you are using it to look something. Invoking a MethodInfo is more expensive than calling a function, but not ridiculously more. In the example you show, if you cache/memoize `theMethod`, you will be far ahead of the game – Flydog57 Nov 30 '19 at 06:50
  • Yeah, that's what I meant by properly used. Like readability is important. I don't know what you mean by cache/memorize theMethod. If you could show an answer where I can call a method with a string without using MethodInfo. And also explain why MethodInfo is not the best way to approach what I am doing, I'd vote you as best answer – NeoChrisOmega Nov 30 '19 at 07:02
  • Sorry on my phone tonight. Easy to comment, hard to answer. What I mean is that if you need to use Reflection, do the `GetType` \ `GetMethod` work once (if possible), remember the result, and use that result over and over (`Invoke`-ing the `MethodInfo` over and over). That way, you amortize the cost of the Type and Method lookup over multiple calls. – Flydog57 Nov 30 '19 at 07:17
  • It has its place, but if you have a way of calling the method without reflection, there is no point in using it; at the very least it's extra work/code, and likely also a performance hit depending on how you do it. – Herohtar Nov 30 '19 at 09:15

1 Answers1

0

You should only use that (...MethodInfo theMethod = thisType.GetMethod(TheCommandString) when you don't have access to the method (TheCommandString) or the class , so it mostly can be used in building something like DI , or in libraries like newtonsoft or automapper.

so in your example if you have access to your method (TheCommandString) you should directly call that ( object.TheCommandString(userParameters)) , if you don't have access (eg : its a private method inside a dll) then you can use reflection.

Ramin
  • 87
  • 2
  • 7