3 ms·
Isn't reflection by definition unstable?
by vadfa 5y ago
Isn't reflection by definition unstable?
- GordonS 5y agoNot so it would crash the entire app with an uncatchable exception, no.
- dtech 5y agoNo. Reflection is a program accessing or modifying its own program structure. There's not need for it to be unstable, languages like Lisp, Java and I assume C# have clearly defined semantics for it.
- SideburnsOfDoom 5y agoCorrect. Perhaps parent comment meant "unstable" in the sense that it turns compile time failures into runtime ones: e.g. without reflection, if you type "customer.GetOrders()" then it either compiles or does not, whereas reflection code that finds a method called "GetOrders" can compile just fine but you won't know if it finds a method of that name or returns null, until runtime.
- phillipcarter 5y ago> There's not need for it to be unstable In practice there is though, it just depends on what you choose to take a dependency on. For example, a few years ago the C# compiler did some lambda function optimization work. This broke someone's code because they were using reflection, and ultimately depended on how lambdas were getting optimized prior to the performance improvement in the compiler. The team by-designed that regression, since they make no guarantees that you can depend on a particular implementation detail of how the compiler optimizes things. That said, when people use reflection in .NET, they're almost always programming against something that is stable and has likely worked the same way for a decade.
- phillipcarter 5y agoAlso, I can't believe I didn't mention this already: Reflection in .NET lets you dynamically invoke anything declared internal or private as well. I think it goes without saying that your code can be broken in the future if you do this.
- Ygg2 5y agoIt's the same as in Java. Java never broke this weirdly though.
- dtech 5y agoYou're confusing safety with stability. Reflection is necessarily unsafe, but not necessarily unstable. You just need to take care about keeping within bounds of guaranteed behavior. E.g. in Java calling a method through reflection is guaranteed by the language to work. 100% stable forever. Reflection also allows you to call internal JVM methods. This might or might not work depending on the JVM, making reflection an unsafe feature. It's still stable on the JVM's is works on though.
- phillipcarter 5y ago> You're confusing safety with stability. Potayto, potahto I guess.