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


    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 { getset; }

            
    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);
            }
        }
    }
  • 相关阅读:
    OpenVINO Model Server的服务化部署——step3(django服务构建)
    (5)名称空间 namespace 和 using 声明
    (4)#include 指令
    (3)注释
    (2)简单的程序
    (1)Hello World
    javaScript 错误学习 -- throw、try 、catch和 finally
    js 如何在数字前面自动补零,生成序列号、单据号
    vs2015项目运行出现“无法启动IIS Express Web服务器”,如何解决
    Sql Server 2008 如何将数据表导出Excel文件?
  • 原文地址:https://www.cnblogs.com/xhan/p/1524035.html
Copyright © 2020-2023  润新知