Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Is Reflection Terrible?

Name: Anonymous 2011-11-04 14:27

Summary: My coworker tells me not to use reflection because it makes code hard to understand; I disagree.

Research: A precursory google search of 'programming is reflection bad' I find that a lot is written about how there can be a large performance impact (and, stupid me, I realized that I was making one of the mistakes listed in some post, but someone listed a simple performance enhancement so I can fix that.) A few people complain about debugging and IDEs not being able to work well with it.

However, and this is the foundation of why I think the whole entire thing is stupid: I am working on a WPF application (C# XML based paradigm for GUI applications) that *USES* reflection to create the structures dynamically anyways! Furthermore, after I built an application with dynamic binding of model objects to view objects, and showing how much cleaner the code looks (no manually creating tables, not having to write complicated validation logic and little workarounds to translate input in the front end to the model object, significantly less lines of code, and less buggy code due to not hand making ideas built right into the libraries like modal windows, data translation, etc.), my coworkers took it as a model for how all WPF applications should be written from then on.

But this is all *GASP* done through the magic of reflection. I do not have a single line of code that I have written that includes "System.Reflection." but obviously the MS Libraries are reflecting to, for example, understand how to make a table with a column for each property of a model object, or knowing that number fields should automatically default to having validation that checks that a number has no invalid characters, and fits inside range int.min and int.max...

But, now, because it uses reflection to create the UI, all I have access to in a datagrid is a string containing the name of the property that that column is bound to. The only way (I can think of,) using this information to get a collection of all the values in that given column is to get a delegate of the getter method for that property and invoke it through reflection. Either that, or write a method for every column of every single attribute of every single object that will ever be used.

So... reading that,

1) is there actually a way to get a collection based off a column without reflection (if someone is familiar with WPF/c#, I have access to an IEnumerable<foo> from which the table is constructed, I was thinking maybe something in Linq might work, or some kinda lambda expression, except, like I said before, the only thing I have access to is a string of the property's name)

2) Is reflection really the Anti-Christ?

Name: Anonymous 2011-11-04 14:36

Reflection is bad. Use inline assembly and pointer hacks instead.
wait.. what?

Name: Anonymous 2011-11-04 14:50

I'm not used to this kind of C# gibberish but if such reflection has a run-time cost then it's no good. If I understand correctly, what you want to do could be easily done with compile-time macros and you'd better use them.

Name: Anonymous 2011-11-04 16:23

I dunno what the run time cost might be... it is certainly not noticeably slow when running this on (an admittedly) tiny amount of sample data. To give some context, all I want to do is get all the values in a column when a button is pushed on a column header... these values are used to dynamically construct a list of filters to choose from (ala MS Excel's filters) that are then put in a drop down menu that extends from the button... The current logic is something like

1) push button, fire handler with a parameter that includes the column header object.

2) Get the name of the bound property from the column. (IE if a table is generated from a list struct of people objects that have  a string name, int idNumber, and DateTime birthday, and you click the idNumber column's button, you would get the string "idNumber" in the logic.

3) using reflection, obtain the type from the structure used to build the table, then look for the property's accessor method using that string.

4) for each item in the list of objects used to construct the table, invoke the get method on that property, and add the value to a list structure. (ie, for each person in list, invoke idNumber's get method, and put that value into a list.)

5) construct the drop down filter menu from this list.

Name: Anonymous 2011-11-05 9:43

Reflection is a powerful feature when used correctly, and would be very handy to have on my current project, but unfortunately I cannot use it, because I am a vampire. ;_;

Name: Anonymous 2013-09-01 14:02


The product of cardinals comes from the cartesian product.


    |X| · |Y| = |X × Y|

Name: Anonymous 2013-09-01 14:33

Reflection (or introspection) IS AMAZING! Specially in big projects.

Name: Anonymous 2013-09-01 14:35

>>7
Introspection (or introversion) IS AMAZING! Specially for autistic people.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List