i red on internet about reflection, i have understood a few codes too, but the problem is that i am really unable to get what is the use of reflection and where should i use it. could some body please explain me about this using an exam开发者_运维问答ple.
For providing examples, I cannot do a better job than google. If you can read this page you can google. But the usage includes:
1) WPF: all the binding is based on reflection, so the framework uses reflection to do teh job. It is how it will bind to a property you define in the XAML.
2) Pluggins: Loading an assembly and looping through the types and perhaps creating an object of a specific type and running a method on it.
3) ASP.NET MVC: Routing is based on reflection
From the documentation:
The classes in the System.Reflection namespace, together with System.Type, enable you to obtain information about loaded assemblies and the types defined within them, such as classes, interfaces, and value types. You can also use reflection to create type instances at run time, and to invoke and access them. For topics about specific aspects of reflection, see Related Topics at the end of this overview.
For example, say that you have a method that takes a parameter of type object. You know that the object has a property called Name
but you don't know the concrete type of the object so you cannot cast it. But, using reflection you can still get at that property:
public string GetName(object o)
{
string name = (string) o.GetType().GetProperty("Name").GetValue(o, null);
return name;
}
Where you "should" use it is where you need to use it. Reflection is one of those things where if you don't need it, you don't notice its existence, but when you DO need it, you're so glad it's there.
Reflection is, simply put, a way of discovering and examining code at runtime that is not available at compile time. If everything you need in your application is available at compile time, you'll never need reflection. But, if you're writing applications that are easy to extend and modify, you'll be using plenty of reflection.
Here's an example: Workflow Foundation 4. In order to create a workflow component, you would normally have to have a deep understanding of how WF4 operates in order to configure your component correctly when the CacheMetadata method is called by the runtime. In order to ease creating components, the WF team implemented the base method of CacheMetadata so that it uses reflection to examine how a component is implemented (what In and OutArguments and Variables<T> it has). This way, the base implementation can configure your Activity correctly with the runtime without your having to lift a finger. This is something that could not be done when the base Activity class was compiled months and months ago, as your Activity didn't exist yet.
Short:
Reflection is a means where you from code could query an object for names properties or methods.
For example a UserControl used in an APS.NET page can check if the masterpage has a DB property for connecting to a database and if so use it, otherwise create its own connection.
That way it does not create unnecessary connections while still being easy to use in pages without master.
精彩评论