Why is reflection recommended in .NET?

Is it definitely a good practice to use it?

What are the possible situations in the project that need reflection?

+53
reflection c #
Sep 22 '09 at 5:30
source share
9 answers

The primary value of Reflection is that it can be used to test assemblies, types, and elements. This is a very powerful tool for determining the contents of an unknown assembly or object and can be used in a variety of cases.

Opponents of Reflection will refer to the fact that it is slow, which is true compared to static code execution, however Reflection is used throughout the .NET platform, and provided that it does not abuse, this can be a very powerful tool in the toolbox.

Some useful applications:

  • Defining build dependencies

  • Location types that match the interface are inferred from the base / abstract class and search for elements by attributes

  • (Smelly) testing - If you depend on a class that is not tested (i.e., it does not allow you to easily create fakes), you can use Reflection to push fake values ​​in the class - this is not very, and not recommended, but it can Be a handy snap tool.

  • Debugging - resetting the list of loaded assemblies, their references, current methods, etc.

+31
Sep 22 '09 at 5:43
source share

There are many uses for reflection:

  • Iterate through object properties.
  • Calling a method defined at runtime.
  • Many other others in the same vein.

However, one of my favorite uses of reflection is to find properties marked with attributes.

For example, I wrote attributes that indicate which properties in my classes should be indexed using Lucene. At run time, I can look at any class and find out which fields need to be indexed by simply querying the class for the "marked" properties.

+28
Sep 22 '09 at 5:39
source share

Reflection is just a way of exploring objects at runtime. You should not use it unless you need to do it.

+19
Sep 22 '09 at 5:42
source share

Reflection allows the application to collect information about itself, as well as manipulate itself. It can be used to search for all types in an assembly and / or dynamically call methods in an assembly.

System.Reflection: the namespace contains classes and interfaces that provide a controlled representation of loaded types, methods and fields with the ability to dynamically create and call types; this process is known as reflection in the .NET framework.

The System.Type: class is the main class for .NET Reflection functions and is the main way to access metadata. The System.Type class is an abstract class and is a type in the Common Type System (CLS).

It presents type declarations: class types, interface types, array types, value types, enumeration types, type parameters, generic type definitions, and open or closed constructed generic types.

For example:

using System; using System.Reflection; static class ReflectionTest { public static int Height; public static int Width; public static int Weight; public static string Name; public static void Write() { Type type = typeof(ReflectionTest); // Get type pointer FieldInfo[] fields = type.GetFields(); // Obtain all fields foreach (var field in fields) // Loop through fields { string name = field.Name; // Get string name object temp = field.GetValue(null); // Get value if (temp is int) // See if it is an integer. { int value = (int)temp; Console.Write(name); Console.Write(" (int) = "); Console.WriteLine(value); } else if (temp is string) // See if it is a string. { string value = temp as string; Console.Write(name); Console.Write(" (string) = "); Console.WriteLine(value); } } } } class Program { static void Main() { ReflectionTest.Height = 100; // Set value ReflectionTest.Width = 50; // Set value ReflectionTest.Weight = 300; // Set value ReflectionTest.Name = "ShekharShete"; // Set value ReflectionTest.Write(); // Invoke reflection methods } } Output Height (int) = 100 Width (int) = 50 Weight (int) = 300 Name (string) = ShekharShete 
+13
May 29 '12 at 7:41 am
source share

You can use reflection to implement a plugin system, for example. You simply search the entire DLL in the folder and check for reflection if they implement a specific plugin interface. This is the main purpose for which I used reflection, but I also used it to implement universal serialization of the home brew object, where performance was not the biggest problem.

+8
Sep 22 '09 at 5:52
source share

As mentioned above, performance will be a success.

Another big advantage is that you can dynamically load assemblies, manipulate properties even if you don’t have the ability to see what to change, etc.

The reasons for using this are many. Here's an introduction if you need to.

+5
Sep 22 '09 at 5:40
source share

Reflection is commonly used in IoC containers. Let's say you want to register each specific class, ending with the word "Controller". Reflection makes a piece of cake.

I also used reflection to manipulate private fields when testing modules.

+4
Sep 22 '09 at 5:42
source share

The very useful XmlSerialization class is reflection based. You do not need to deal with the thought of using serialization; serialization classes are self-reflecting. But it helps to tag your code with Attributes that determine how objects are serialized. Serialization classes use runtime reflection to read these attributes. In the end, the process seems almost magical, requiring very few lines of explicit encoding in the application; it is a reflection that makes this convenience possible.

XmlSerialization itself is amazing, not only because it is a very convenient way to create data files from an application, but also a very simple way to generate readable records of the program’s internal data model for debugging purposes.

+1
Feb 05 '13 at 21:11
source share

Based on C ++ and requiring simple class hierarchies, I can say that the is keyword is invaluable!

 class MenuItem : Item { } foreach(Item items in parent.ChildItems) { if (item is MenuItem) { /* handle differently */ } } 

PS Isn't that a very expensive attitude, by the way?

0
Sep 22 '09 at 5:51
source share



All Articles