Why is the use of reflection in .NET recommended?

后端 未结 9 625
再見小時候
再見小時候 2020-11-30 22:25

Is it definitely a good practice to use it?

What are some possible situations in a project that need reflection?

相关标签:
9条回答
  • 2020-11-30 23:07

    Reflection allows an application to collect information about itself and also to manipulate on itself. It can be used to find all types in an assembly and/or dynamically invoke methods in an assembly.

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

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

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

    Eg:

    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
    
    0 讨论(0)
  • 2020-11-30 23:08

    You can use reflection to implement a system of plugins for example. You just look for all DLL's in a folder and through reflection check if they implement a certain plugin interface. This is the main purpose for which I used reflection, but I also used it to implement a generic home-brew object serialization, where performance was not the greatest concern.

    0 讨论(0)
  • 2020-11-30 23:10

    Reflection is commonly used in IoC containers. Let's say you want to register every concrete class the ends with the word "Controller". Reflection makes that a piece of cake.

    I've also used reflection to manipulate private fields when unit testing classes.

    0 讨论(0)
提交回复
热议问题