Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C # reflection

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use C# reflection". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use C# reflection.

What are the meanings of explicit and implicit?

Explicit and implicit belong to conversion operators. If we use both of them, our custom types can be exchanged with each other. Explicti means explicit conversion, such as forced conversion from A-> B (B = (B) A), implicit means implicit conversion, such as direct assignment from B-> A (A = B).

Implicit conversion can make our code look prettier and easier to understand, so * use the implicit operator more often. But! What's the use if the object itself loses some params during conversion?

The params keyword is used in the parameter list of method members, providing the method with the ability to have a variable number of parameters before it can be defined only once and cannot be defined after that.

Example:

Using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 {class App {/ * parameters must be integers, but the number of subsequent parameters is variable. / / and since the object array is specified, all data types can be passed as parameters public static void UseParams (int id, params object [] list) {Console.WriteLine (id); for (int I = 0; I < list.Length; iArray +) {Console.WriteLine (list [I]) } static void Main () {/ / variable parameter part passes three parameters, all of which are string type UseParams (1, "a", "b", "c"); / / variable parameter part passes four parameters, which are string, integer, floating-point number and double-precision floating-point array UseParams (2, "d", 100,33.33, new double [] {1.1,2.2}); Console.ReadLine () }

What is a reflex?

Reflection, Reflection, through which we can get all kinds of information at run time, such as assemblies, modules, types, fields, properties, methods, and events, and perform operations on types after they are dynamically instantiated

It is generally used for the implementation of plug-in framework programs and design patterns, of course, reflection is a means to make full use of its energy to accomplish whatever you want to do (it seems to have seen a master call a function not specified in an official class library with reflection. )

Example:

Using System

Using System.Collections.Generic

Using System.Text

Namespace Example25Lib {

Public class Class1 {

Private string name;private int age

/ / if the parameterless constructor is explicitly declared, the client only needs to instantiate the class with the assembly's CreateInstance

/ / it is deliberately not implemented here to reflect the reflective implementation of the constructor on the client side / / public Class1 ()

}

Public Class1 (string Name, int Age)

{name = Name;age = Age;} public void ChangeName (string NewName)

{name = NewName;} public void ChangeAge (int NewAge)

{age = NewAge;} public override string ToString ()

{return string.Format ("Name: {0}, Age: {1}", name, age)

}

Using System

Using System.Collections.Generic

Using System.Text

/ / pay attention to add the namespace using System.Reflection of the reflection

Namespace Example25 {class Program {static void Main (string [] args)

{/ / load assembly Assembly tmpAss = Assembly.LoadFile

(AppDomain.CurrentDomain.BaseDirectory + "Example25Lib.dll")

/ / iterate through all types in the assembly and instantiate Type [] tmpTypes = tmpAss.GetTypes ()

Foreach (Type tmpType in tmpTypes)

{/ / get constructor information of * types ConstructorInfo []

TmpConsInfos = tmpType.GetConstructors ()

Foreach (ConstructorInfo tmpConsInfo in tmpConsInfos)

{/ / generate the called parameter set ParameterInfo for the constructor []

TmpParamInfos = tmpConsInfo.GetParameters ()

Object [] tmpParams = new object [tmpParamInfos.Length]

For (int I = 0; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report