DEV Community

Cover image for Coding Concepts - Reflection

Coding Concepts - Reflection

Chris Bertrand on July 05, 2018

Reflection - What you need to know. In computer science, reflection is the ability of a computer program to examine, introspect, and modify its ow...
Collapse
 
kayis profile image
K

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.

Collapse
 
chris_bertrand profile image
Chris Bertrand

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...

Collapse
 
nans profile image
Nans Dumortier

Thanks Chris for the reference to ponyfoo.com ! I didn't know that website, and will definitely hang out there more often !

Thread Thread
 
chris_bertrand profile image
Chris Bertrand

It's great right! Looks like it's had a visual overhaul since I posted this too! Looks even better now.

Thread Thread
 
nans profile image
Nans Dumortier

Yup, and it's a PWA too so I feel even better browsing it 😁

Collapse
 
rafalpienkowski profile image
Rafal Pienkowski

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.

Collapse
 
chris_bertrand profile image
Chris Bertrand

Thanks Rafal!

Collapse
 
usmansabir profile image
Usman Sabir

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());

Collapse
 
chris_bertrand profile image
Chris Bertrand

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!

Collapse
 
jude00861082 profile image
Jude

Nice IDE themes 😍
Where can I get it?
.
Thanks

Collapse
 
chris_bertrand profile image
Chris Bertrand

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...