using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespace GetPrivateMemberTestA { class myc { string name = "my name"; string address = "my address"; int age = 23; DateTime birthday = new DateTime(1986, 5, 18); public override string ToString() { return "name = " + name + "\r\n" + "address = " + address + "\r\n" + "age = " + age + "\r\n" + "birthday = " + birthday + "\r\n"; } } class Program { static void Main(string[] args) { myc c = new myc(); Console.WriteLine("ToString : \r\n-------------------------------------------------------------"); Console.WriteLine(c.ToString()); FieldInfo[] fields = c.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic); Console.WriteLine("FieldInfo[] : \r\n-------------------------------------------------------------"); foreach (FieldInfo field in fields) { Console.WriteLine(String.Format("{0} : {1}", field.Name, field.GetValue(c))); } Console.ReadKey(); } } }输出:ToString : ------------------------------------------------------------- name = my name address = my address age = 23 birthday = 1986-5-18 0:00:00FieldInfo[] : ------------------------------------------------------------- name : my name address : my address age : 23 birthday : 1986-5-18 0:00:00
即使获取出来,又有什么用呢?lz要它做什么?
你想得到变量名?貌似没意义,发布的软件都要混淆的,发布后变量名都变了,这样反射没意义。而且,貌似没办法。编译后的IL中是否还有这个myWeb?貌似没有了吧。IL中还有变量的名字么?
public object a,b;
private void button1_Click(object sender, System.EventArgs e)
{
a = 1;
b = 1;
MessageBox.Show(GetName(ref a));
}
private string GetName(ref object x)
{
object t = a;
if (t == x ) return "a ";
t = b;
if (t == x ) return "b ";
return x.ToString(); }
{
public int MyProperty { get; set; } }
//在这里面给你的sting变量赋值。。
在类中就可以得到属性名称了。。
MyStruct aaa = new MyStruct();
aaa.MyProperty;
hi,peter 你给的这个链接 我看不懂最后那段C++ ⊙﹏⊙b汗
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace GetPrivateMemberTestA
{
class myc
{
string name = "my name";
string address = "my address";
int age = 23;
DateTime birthday = new DateTime(1986, 5, 18); public override string ToString()
{
return "name = " + name + "\r\n"
+ "address = " + address + "\r\n"
+ "age = " + age + "\r\n"
+ "birthday = " + birthday + "\r\n";
}
}
class Program
{
static void Main(string[] args)
{
myc c = new myc();
Console.WriteLine("ToString : \r\n-------------------------------------------------------------");
Console.WriteLine(c.ToString());
FieldInfo[] fields = c.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
Console.WriteLine("FieldInfo[] : \r\n-------------------------------------------------------------");
foreach (FieldInfo field in fields)
{
Console.WriteLine(String.Format("{0} : {1}", field.Name, field.GetValue(c)));
}
Console.ReadKey();
}
}
}输出:ToString :
-------------------------------------------------------------
name = my name
address = my address
age = 23
birthday = 1986-5-18 0:00:00FieldInfo[] :
-------------------------------------------------------------
name : my name
address : my address
age : 23
birthday : 1986-5-18 0:00:00
{
public string str = "spp";
public string spp = "very good"; public string myWeb = "abc";
public string myWeb2 = "abc"; public Form1()
{
InitializeComponent();
string str1 = GetName(2); //str1="myWeb"
string str2 = GetName(3); //str2="myWeb2"
string str3 = this.GetType().GetField(str).GetValue(this).ToString(); //str3="very good"
}
public string GetName(int i)
{
System.Reflection.FieldInfo[] fields = this.GetType().GetFields();
return fields[i].Name;
}
}