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

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);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值