首页  编辑  

结构类型使用FieldInfo.SetValue方法

Tags: /C#/RTTI/   Date Created:

using System . Reflection;

public struct Test

{

    public string Name;

};

private void button1_Click( object sender , EventArgs e)

{

    object Temp;

    FieldInfo vFieldInfo = typeof ( Test ) . GetField( "Name" );

   Temp = new Test ();

   vFieldInfo . SetValue(Temp, "Zswang );

   Text = (( Test )Temp) . Name;

}

// or

private void button2_Click( object sender , EventArgs e)

{

    Test Temp;

    FieldInfo vFieldInfo = typeof ( Test ) . GetField( "Name" );

   Temp = new Test ();

    object P = ( object )Temp;

   vFieldInfo . SetValue(P, "Zswang );

   Temp = ( Test )P;

   Text = Temp . Name;

}