Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What use is Reflection in .NET? [duplicate]

Possible Duplicates:
When do you use reflection? Patterns/anti-patterns
What exactly is Reflection and when is it a good approach?

What is the need for reflection in .NET? What are the situations where it comes in handy?

like image 623
Milen Avatar asked May 25 '09 05:05

Milen


People also ask

What is the purpose of reflection in C#?

Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.

What is reflection used for?

Reflection gives us information about the class to which an object belongs and also the methods of that class which can be executed by using the object. Through reflection we can invoke methods at runtime irrespective of the access specifier used with them.

What is reflection in net please describe a use case and the benefits of using it?

Reflection enables you to use code that was not available at compile time. . NET Reflection allows application to collect information about itself and also manipulate on itself. It can be used effectively to find all the types in an assembly and/or dynamically invoke methods in an assembly.

What is reflection in C# stackoverflow?

Reflection lets your code call methods and properties that you didn't know about when the code was compiled.


1 Answers

Let's say you're writing a basic serialization routine, that will serialize any object to XML. How would you make it generic enough, so that it can work for any object? If you have a class where you know all the properties, then you can easily write a "ToXml()" function, where you manually write out all the properties to XML. What if you want to extend this though to ANY object? In that case, you need to reflect over the properties at runtime, and write them out to the XML.

There are many more uses for it, that's the first one that came to mind.

like image 111
BFree Avatar answered Sep 20 '22 02:09

BFree