Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use reflection reflection

Shulou Source: shulou.com Published: 2022-06-02 08:16:27 10月04日 Update

This article mainly introduces "the use of reflection reflection". In daily operation, I believe many people have doubts about the use of reflection reflection. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the use of reflection reflection". Next, please follow the editor to study!

/ / reflection

Public static void Reflection ()

{

/ / call non-static methods

/ / 1.Load (namespace name), GetType (namespace. Class name)

Type type = Assembly.Load ("ConsoleApp") .GetType ("ConsoleApp.Program"); / / dynamically load dll and get the type

/ / 2.GetMethod (the name of the method to be called)

MethodInfo method = type.GetMethod ("GetReflect", new Type [] {typeof (string), typeof (int)})

/ / 3. The instantiated method called needs to create an instance of the type

Object obj = Activator.CreateInstance (type)

/ / 4. Method requires the parameters passed in

Object [] parameters = new object [] {"xxx", 10}

/ / 5. Call method

String result = (string) method.Invoke (obj, parameters)

/ / call static method

Type.InvokeMember ("GetReflect", System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Static |

System.Reflection.BindingFlags.Public, null, null, new object [] {"sss", 15})

/ / get the method name

MethodInfo [] info = typeof (Program) .GetMethods

Foreach (MethodInfo item in info)

{

Console.WriteLine (item.Name)

Var parames = item.GetParameters ()

Foreach (var parame in parames)

{

Console.WriteLine (parame.Name + "- -" + parame.ParameterType.Name)

}

}

/ / get the properties and types of the object

Type tt = typeof (User)

Var fields = tt.GetProperties ()

Foreach (var item in fields)

{

Console.WriteLine (item.Name + ":" + item.GetValue (user, null) + ":" + item.PropertyType)

}

/ / get the property of the object and assign it, and call the method in the class

Type projectType = typeof (User)

Object projectInstance = Activator.CreateInstance (projectType)

PropertyInfo propertyName = projectType.GetProperty ("name")

PropertyName.SetValue (projectInstance, Xiao Ming)

Var projectName = propertyName.GetValue (projectInstance)

PropertyInfo propertyAge = projectType.GetProperty ("age")

PropertyAge.SetValue (projectInstance, 99, null)

Var projectAge = propertyAge.GetValue (projectInstance)

Console.WriteLine ("name: {0} Age: {1}", projectName, projectAge)

MethodInfo method1 = projectType.GetMethod ("GetReflect", new Type [] {typeof (string), typeof (int)})

Object [] parsArray = {"Wu Dalang", 55}

Method1.Invoke (projectInstance, parsArray)

}

Public static string GetStaticReflect (string name, int age)

{

Console.WriteLine ("reflection executed" + name + "this year" + age + "years old")

Return name + "123"

}

Public class User

{

Public string name {get; set;}

Public int age {get; set;}

Public void GetReflect (string name, int age)

{

Console.WriteLine ("User reflection executed" + name + "this year" + age + "years old")

}

}

At this point, the study on "the use of reflective reflection" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Method reflection usage learning name type instance object property more space static help utility next dynamic parameter name age article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Information vpn Shulou Tech Info Linux