How to operate generics and attribute fields in C #
Most people do not understand the knowledge points of this article "how to operate generics and attribute fields in C#", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "how to operate generics and attribute fields in C#" article.
I. method of use
Find the DLL file
Manipulate dll files through various methods in the Reflection reflection class library
II. Steps
Load DLL Fil
Assembly assembly1 = Assembly.Load ("SqlServerDB"); / / method 1: this DLL file should be string filePath = Environment.CurrentDirectory + ""; Assembly assembly2 = Assembly.LoadFile (filePath + @ "\ SqlServerDB.dll"); / / method 2: full path Assembly assembly3 = Assembly.LoadFrom (filePath + @ "\ SqlServerDB.dll"); / / method 3: full path Assembly assembly4 = Assembly.LoadFrom (@ "SqlServerDB.dll"); / / method 3: full path
Get the specified type
Foreach (var item in assembly4.GetTypes ()) / / finds all types, that is, how many classes {Console.WriteLine (item.Name);}
Get constructor
Type type = assembly4.GetType ("SqlServerDB.ReflectionTest"); / / call foreach (var ctor in type.GetConstructors (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) {Console.WriteLine ($"Constructor: {ctor.Name}") in the ReflectionTest class; foreach (var param in ctor.GetParameters ()) {Console.WriteLine ($"Constructor parameters: {param.ParameterType}");} / / [3] instantiate / / ReflectionTest reflectionTest = new ReflectionTest () / / this instantiation is to know the specific type-static / / object objTest = Activator.CreateInstance (type); / / dynamic instantiation-call our constructor object objTest1 = Activator.CreateInstance (type, new object [] {"string"}); / / dynamic instantiation-call our parameterized constructor / / call private constructor / / ReflectionTest reflectionTest = new ReflectionTest (); / / normal call object objTest2 = Activator.CreateInstance (type, true)
Call non-constructor methods
Object objTest2 = Activator.CreateInstance (type, true); / / the advantage of calling the ordinary method ReflectionTest reflectionTest = objTest2 as ReflectionTest;//as conversion, which returns nullreflectionTest.Show1 () if the error is not reported; / / calls the private method var method = type.GetMethod ("Show2", BindingFlags.Instance | BindingFlags.NonPublic); method.Invoke (objTest2, new object [] {})
Call a generic method
/ / generic no parameter var method3 = type.GetMethod ("Show3"); / / find specified method var genericMethod = method3.MakeGenericMethod (new Type [] {typeof (int)}); / / specify generic parameter type TgenericMethod.Invoke (objTest2, new object [] {}); / / generic parameter var method4 = type.GetMethod ("Show4"); / / find specified method var genericMethod4 = method4.MakeGenericMethod (generic [] {typeof (string)}) / / specify generic parameter type TgenericMethod4.Invoke (objTest2, new object [] {123, "generic string parameter"})
Reflection test class
Located in the ReflectionTest.cs file in SqlServerDB.dll
/ reflection test class / public class ReflectionTest {/ / private constructor private ReflectionTest () {Console.WriteLine ("this is a private parameterless constructor") } / / ordinary constructor / / public ReflectionTest () / / {/ / Console.WriteLine ("this is a no-parameter construction method"); / /} public ReflectionTest (string name) {Console.WriteLine ($"this is a parameter construction method + parameter values are: {name}") } public void Show1 () {Console.WriteLine ("call normal method", this.GetType ());} private void Show2 () {Console.WriteLine ("call private method", this.GetType ()) } public void Show3 () {Console.WriteLine ("calling parameterless generic methods", this.GetType ());} public void Show4 (int id,string name) {Console.WriteLine ($"calling parameterized generic methods with parameters {id}, {name}", this.GetType ());}} manipulating generic classes and generic methods
Load DLL Fil
Assembly assembly = Assembly.LoadFrom (@ "SqlServerDB.dll")
Get the specified type
Type type = assembly.GetType ("SqlServerDB.GenericClass`2") .MakeGenericType (typeof (int), typeof (string)); / / be sure to give specific type parameters
Call a generic method
Object objTest2 = Activator.CreateInstance (type); var method = type.GetMethod ("GenericMethod") .MakeGenericMethod (typeof (int)); method.Invoke (objTest2, new object [] {})
Reflection test class
Located in the GenericClass.cs file in SqlServerDB.dll
Public class GenericClass {public void GenericMethod () {Console.WriteLine ("generic class invocation + generic method");} Action class property field
Load DLL Fil
Assembly assembly2 = Assembly.LoadFrom ("SqlServerDB.dll")
Get the specified type
Type type2 = assembly2.GetType ("SqlServerDB.PropertyClass")
Call a generic method
Object obj = Activator.CreateInstance (type2); foreach (var property in type2.GetProperties ()) {Console.WriteLine (property.Name); / / set the value to the property if (property.Name.Equals ("Id")) {property.SetValue (obj, 1);} else if (property.Name.Equals ("Name")) {property.SetValue (obj, "Learning programming") } else if (property.Name.Equals ("Phone")) {property.SetValue (obj, "123459789");} / / get the attribute value Console.WriteLine (property.GetValue (obj));}
Reflection test class
Located in the PropertyClass.cs file in SqlServerDB.dll
Public class PropertyClass {public int Id {get; set;} public string Name {get; set;} public string Phone {get; set;}} above is the content of this article on "how to operate generics and attribute fields in C#". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.