Reflection - What you need to know.
In computer science, reflection is the ability of a computer program to examine, introspect, and modify its ow...
For further actions, you may consider blocking this person and/or reporting abuse
Good writeup :)
After using reflection in Java and PHP I had the impression that JavaScript is even better in that way, because classes are implemented dynamically and can be accessed without extra magic, almost homoiconic.
JavaScript and reflection is an interesting concept, as JavaScript doesnt really have the notion of classes,as everything is an object and already being a dynamic language things get a bit murky.
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.
Thanks Rafal!
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=2.0.0.0, 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.
System.Console.WriteLine(integerInstance.MaxValue());
Hey Usman,
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
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...