Good writeup :)
But ES5 and more so ES6 do contain a Reflection Api. This great article goes way more in depth on the topic then i ever could: ponyfoo.com/articles/es6-reflectio...
Thanks Chris for the reference to ponyfoo.com ! I didn't know that website, and will definitely hang out there more often !
It's great right! Looks like it's had a visual overhaul since I posted this too! Looks even better now.
Yup, and it's a PWA too so I feel even better browsing it 😁
Nice article Chris. Personally, I try to avoid reflection as long as it's possible. As you said, reflection is being resolved at runtime and if I made a mistake in the source code I'll know about it when I run my program on the test, UAT or production environment (this is pretty late in my opinion).
I like your real life example. I think it's very descriptive. If I have to face such a problem I'd like to use such kind of dynamic dependency injection with a configuration provided from a file or a database.
I don't want to say that reflection is an evil but we should use it very carefully because it's a two-edged sword.
Once again great post. Cheers.
2nd code example doesn't work
// Using Reflection to get information from an Assembly:
System.Reflection.Assembly integerTypeAssembly = typeof(System.Int32).Assembly;
System.Console.WriteLine(integerTypeAssembly); // mscorlib, Version=126.96.36.199, Culture=neutral, PublicKeyToken=b77a5c561934e089
// We can then Create a new Instance of whatever this type may be, and call it's methods.
object integerInstance = Activator.CreateInstance(integerTypeAssembly);
// Now we can use integerInstance as if it was an Intger Type, calling its MaxCValue function.
You're very much correct, I should of tested the snippet in .Net Fiddle before posting it. I wrote is as pseudo code, but can see how it could be misinterpreted. I've updated the example with one that actually works. Although the Integer class is a very boring example!
Nice IDE themes 😍
Where can I get it?
Thanks Jude, unfortunately it's just the default markdown stylings. But i do agree it does look good! This guide is pretty cool for learning more about how it works guides.github.com/features/masteri...
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.