您的位置:首页 > 其它

通过反射访问对象私有和保护成员

2010-02-04 13:42 435 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Reflection;
namespace Com.xxx.Utils
{
/// <summary>
/// 单元测试工具类
/// </summary>
public static class UnitTestHelper
{
public static void AssertException<TCallBack>
(string errorMessage,
TCallBack callBackMethod,
params object[] parameters)
{
object method = callBackMethod as object;
bool hasException = false;
try
{
((Delegate)method).DynamicInvoke(parameters);
}
catch (Exception e)
{
hasException = true;
Assert.AreEqual(errorMessage, e.InnerException.Message);
}
if (!hasException)
{
throw new Exception(errorMessage + "异常没有出现!");
}
}
public static void SetNonPublicField(object obj, string fieldName, object newValue)
{
try
{
FieldInfo field = obj.GetType().GetField(fieldName,
BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(obj, newValue);
}
catch (Exception e)
{
throw e.InnerException;
}
}
public static object GetNonPublicField(object obj, string fieldName)
{
try
{
FieldInfo field = obj.GetType().GetField(fieldName,
BindingFlags.NonPublic | BindingFlags.Instance);
return field.GetValue(obj);
}
catch (Exception e)
{
throw e.InnerException;
}
}
public static void SetNonPublicProperty(object obj, string propertyName, object newValue)
{
try
{
PropertyInfo property = obj.GetType().GetProperty(propertyName,
BindingFlags.NonPublic | BindingFlags.Instance);
property.SetValue(obj, newValue, null);
}
catch (Exception e)
{
throw e.InnerException;
}
}
public static object GetNonPublicProperty(object obj, string propertyName)
{
try
{
PropertyInfo property = obj.GetType().GetProperty(propertyName,
BindingFlags.NonPublic | BindingFlags.Instance);
return property.GetValue(obj, null);
}
catch (Exception e)
{
throw e.InnerException;
}
}
public static object CallNonPublicMethod(object obj, string methodName, params object[] args)
{
try
{
MethodInfo method = obj.GetType().GetMethod(methodName,
BindingFlags.NonPublic | BindingFlags.Instance);
return method.Invoke(obj, args);
}
catch (Exception e)
{
throw e.InnerException;
}
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Com.xxx.Utils;
namespace Com.xxx.xxx.Test.Utils
{
public class A
{
private int pri;

protected string pro;

protected string Pro { get; set; }

private string PriMethod(string hello)
{
return hello + " xhan Pri";
}
protected string ProMethod(string hello)
{
return hello + " xhan Pro";
}

}
[TestFixture]
public class UnitTestHelperTest
{
[Test]
public void TestSetGetNonPoublicField()
{
A a = new A();
UnitTestHelper.SetNonPublicField(a, "pri", 1);

int pri = (int)UnitTestHelper.GetNonPublicField(a, "pri");
Assert.AreEqual(1, pri);

UnitTestHelper.SetNonPublicField(a, "pro", "hello");
Assert.AreEqual("hello", UnitTestHelper.GetNonPublicField(a, "pro").ToString());
}
[Test]
public void TestGetSetNonPublicProperty()
{
A a = new A();
UnitTestHelper.SetNonPublicProperty(a, "Pro", "xhan");
Assert.AreEqual("xhan", UnitTestHelper.GetNonPublicProperty(a, "Pro"));
}
[Test]
public void TestCallNonPublicMethod()
{
A a = new A();
string priResult = UnitTestHelper.CallNonPublicMethod(a, "PriMethod", "ni hao").ToString();

Assert.AreEqual("ni hao xhan Pri", priResult);

string proResult = UnitTestHelper.CallNonPublicMethod(a, "ProMethod", "ni hao").ToString();

Assert.AreEqual("ni hao xhan Pro", proResult);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐