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 reflection reflection

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

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!

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

Internet Technology

Wechat

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

12
Report