views:

148

answers:

4

I recently started work at a new company and a .net web application we have is built using reflection. I have only been out of school for a year and haven't worked with this concept. After studying the code... it looks like there is a single backend interface of type object that has about 20 classes that inherit from it. lots of generic gets and sets

On the surface it looks like standard inheritance to me. I guess my question is, what makes this reflection? Is it because the interface is not strongly typed?

Thanks

+1  A: 

Who says it is "built using reflection"? Your coworkers?

Generally speaking, to use reflection means to inspect or generate code at runtime.

If the application is using reflection, it is probably using stuff from the System.Reflection namespace, so to get a feel for the extent to which reflection is used, you could remove references to this namespace and see what breaks...

Rune
They say it is built using reflection and from what my rudimentary understanding of reflection it is a way for objects to deterimine there invocations/methods functionality at run time. I really don't know much about reflection and most of the general explanations on the internet explain it with terminology that I am not real solid on.
sdmiller
Say my program provides your program with some object of which you know nothing. The first thing you could do would be to call runesObject.GetType() to get an object runesType of type System.Type. This object describes the type of runesObject, so you can call methods on runesType to get information on constructors, methods, properties etc. Using this information along with runesObject will enable you to invoke methods and/or properties on runesObject which you didn't even knew existed.
Rune
A: 

"Reflection: The ability to discover the composition of a type (e.g., class, interface, structure, enumeration, or delegate) at runtime."

I believe your coworkers are referring to how they instantiate your objects. I'm sure your objects are built using standard inheritance.

Glennular
+2  A: 

Please read about Reflection on Wikipedia first. Then check out the Reflection Namespace on MSDN.

I believe I know the general pattern that you are encountering. The need for reflection arises as the user can specify type and member names at runtime. The program then has to use reflection to map the supplied names to actual type members.

Generally speaking, if your program inspects object types at runtime, then you are using reflection.

BTW, Strong and Weak are attributes of the language type system, but not of any application or structure ( eg. interface, etc ) written using that language. ie. "C# is considered by many to be a strongly typed language".

kervin
Thanks... I think I am starting to understand. Great links!
sdmiller
You're welcome, glad they helped.
kervin
A: 

a) The application cant be buit using reflection. Reflection is most likely a part of the application. Just like inheritance in the case of one parent and 20 children classes as you mention.

b) Reflection is used to analyze an object at run time. Implying supposing you have an object referrence of the type object and maybe this object type could vary and you want at run time to see al the properties then reflection is used. Another case is when you have to call a method but you dont really know which method and its all general.

c) since its a .net app, in case of vb.net, a line with something like objectinstance.GetType.somemethod wil point to reflection. check it out

Ashraf
Also another use beyond the two mentioned earlier is instantiating objects at run time esp when the type could vary
Ashraf
reflection can be used to build types at runtime based on a runtime determine interface. those types could in and on their own be an application, so technically it's possible to use reflection for (part of the) build process to build an application. Though I'm pretty sure it's not the case here
Rune FS
Ok. Thats a nifty way of doing things I havent been exposed to. Could you point me to a code snippet or a tut demonstrating this.
Ashraf