转自 http://oulehui.blog.163.com/blog/static/79614698201122411454722/
首先我必须承认访问一个类的私有成员不是什么好做法。大家也都知道私有成员在外部是不能被访问的。而一个类中会存在很多私有成员:如私有字段、私有属性、私有方法。对于私有成员访问,可以套用下面这种非常好的方式去解决。
private
string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
但是有时候,源代码是别人的,你就不能修改源代码,只提供给你dll。或者你去维护别人的代码,源代码却有丢失。这样的情况如果你想知道私有成员的值,甚至去想直接调用类里面的私有方法。那怎么办呢?其实在.net中访问私有成员不是很难,这篇文章提供几个简单的方法让你如愿以偿。
为了让代码用起来优雅,使用扩展方法去实现。
1、得到私有字段的值:
public
static T GetPrivateField<T>(
this
object instance,
string fieldname)
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
FieldInfo field = type.GetField(fieldname, flag);
return (T)field.GetValue(instance);
}
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
FieldInfo field = type.GetField(fieldname, flag);
return (T)field.GetValue(instance);
}
2、得到私有属性的值:
public
static T GetPrivateProperty<T>(
this
object instance,
string propertyname)
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
PropertyInfo field = type.GetProperty(propertyname, flag);
return (T)field.GetValue(instance, null);
}
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
PropertyInfo field = type.GetProperty(propertyname, flag);
return (T)field.GetValue(instance, null);
}
3、设置私有成员的值:
public
static
void SetPrivateField(
this
object instance,
string fieldname,
object
value)
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
FieldInfo field = type.GetField(fieldname, flag);
field.SetValue(instance, value);
}
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
FieldInfo field = type.GetField(fieldname, flag);
field.SetValue(instance, value);
}
4、设置私有属性的值:
public
static
void SetPrivateProperty(
this
object instance,
string propertyname,
object
value)
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
PropertyInfo field = type.GetProperty(propertyname, flag);
field.SetValue(instance, value, null);
}
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
PropertyInfo field = type.GetProperty(propertyname, flag);
field.SetValue(instance, value, null);
}
5、调用私有方法:
public
static T CallPrivateMethod<T>(
this
object instance,
string name,
params
object[] param)
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
MethodInfo method = type.GetMethod(name, flag);
return (T)method.Invoke(instance, param);
}
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
MethodInfo method = type.GetMethod(name, flag);
return (T)method.Invoke(instance, param);
}
测试:
下面我们使用一个测试类,进行测试。新建一个类库项目,测试的类代码如下:
public
class TestClass
{
public TestClass()
{
privatefield1 = 1;
privatefield2 = 99;
PrivateFieldA = "Lo";
PrivateFieldB = "ve";
}
private int privatefield1;
private int privatefield2;
private string PrivateFieldA
{
get;
set;
}
private string PrivateFieldB
{
get;
set;
}
private int Add()
{
return privatefield1 + privatefield2;
}
private string Join()
{
return PrivateFieldA + PrivateFieldB;
}
}
{
public TestClass()
{
privatefield1 = 1;
privatefield2 = 99;
PrivateFieldA = "Lo";
PrivateFieldB = "ve";
}
private int privatefield1;
private int privatefield2;
private string PrivateFieldA
{
get;
set;
}
private string PrivateFieldB
{
get;
set;
}
private int Add()
{
return privatefield1 + privatefield2;
}
private string Join()
{
return PrivateFieldA + PrivateFieldB;
}
}
将上面类库的dll引入控制台项目中。使用下面代码去使用这个类的私有成员:
TestClass obj =
new TestClass();
System.Console.WriteLine( "私有字段");
System.Console.WriteLine(obj.GetPrivateField< int>( "privatefield1"));
System.Console.WriteLine(obj.GetPrivateField< int>( "privatefield2"));
System.Console.WriteLine( "私有属性");
System.Console.WriteLine(obj.GetPrivateProperty< string>( "PrivateFieldA"));
System.Console.WriteLine(obj.GetPrivateProperty< string>( "PrivateFieldB"));
System.Console.WriteLine( "私有方法");
System.Console.WriteLine(obj.CallPrivateMethod< int>( "Add", null));
System.Console.WriteLine(obj.CallPrivateMethod< string>( "Join", null));
System.Console.WriteLine( "修改私有属性");
obj.SetPrivateProperty( "PrivateFieldA", "hello");
obj.SetPrivateProperty( "PrivateFieldB", "world");
System.Console.WriteLine(obj.CallPrivateMethod< string>( "Join", null));
System.Console.Read();
System.Console.WriteLine( "私有字段");
System.Console.WriteLine(obj.GetPrivateField< int>( "privatefield1"));
System.Console.WriteLine(obj.GetPrivateField< int>( "privatefield2"));
System.Console.WriteLine( "私有属性");
System.Console.WriteLine(obj.GetPrivateProperty< string>( "PrivateFieldA"));
System.Console.WriteLine(obj.GetPrivateProperty< string>( "PrivateFieldB"));
System.Console.WriteLine( "私有方法");
System.Console.WriteLine(obj.CallPrivateMethod< int>( "Add", null));
System.Console.WriteLine(obj.CallPrivateMethod< string>( "Join", null));
System.Console.WriteLine( "修改私有属性");
obj.SetPrivateProperty( "PrivateFieldA", "hello");
obj.SetPrivateProperty( "PrivateFieldB", "world");
System.Console.WriteLine(obj.CallPrivateMethod< string>( "Join", null));
System.Console.Read();
结果如下: