C# 通过反射如何访问静态成员

//动态链接库中PvsApiIfCtrl.Cls.Cls_Public类有一变量 private static string key="abcd";
//下面通过反射的技术修改和获取值
//设置key
public static void updatePalmKey(string key = "1234567890123456")
{
BindingFlags flag = BindingFlags.Static | BindingFlags.NonPublic;
FieldInfo f_key = typeof(PvsApiIfCtrl.Cls.Cls_Public).GetField("key", flag);
f_key.SetValue(new PvsApiIfCtrl.Cls.Cls_Public(), key);
}

///
/// 获取key
///

///
public static string getPalmKey()
{
BindingFlags flag = BindingFlags.Static | BindingFlags.NonPublic;
FieldInfo f_key = typeof(PvsApiIfCtrl.Cls.Cls_Public).GetField("key", flag);
object o = f_key.GetValue(new PvsApiIfCtrl.Cls.Cls_Public());
return o.ToString();
}

你可能感兴趣的:(C#)