Understanding On Reflection


———————————- Reflection dynamically creates a class object in an assembly that dynamically executes methods in a class , Attributes, fields

1, the assembly is divided into. Dll and .exe

2, the correspondence between assembly and methods of reflection

  1. class corresponds to Type
  2. field corresponds to FieldInfo
  3. property corresponding tp PropertyInfo
  4. method corresponds to MethodInfo

 

#region 1.0

Reflection: dynamic assign the value to property of Pig class

private void button1_Click(object sender, EventArgs e)
{
//Pig pig = new Pig();
//pig.Name = “pigpig”;
//pig.Name

//1.0 Load the assembly of the pig class lib.dll into memory
Assembly ass = Assembly.LoadFile(@”path”);

//2.0 create pig object dynamically
object pigInstance = ass.CreateInstance(“Lib.Pig”); // like Pig pig= new Pig();

//3.0 give value to name of pig
Type PigType = ass.GetType(“Lib.Pig”);

PropertyInfo nameInfo = PigType.GetProperty(“Name”); //pig.Name
nameInfo.SetValue(pigInstance, “pig_king”);

}
#endregion

 

Social Media


Categories



All Posts



Contact Info