zoukankan      html  css  js  c++  java
  • 通过反射访问对象私有和保护成员

    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);
            }
        }
    }
  • 相关阅读:
    如何使用plsql导出oracle中的建表语句文件
    启动Tomcat 7.0 报 Server Tomcat v7.0 was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.
    Spring + springMvc + MyBatis 整合步骤(收藏)
    同台电脑部署多组Tomcat负载均衡(或集群)
    Apache+Tomcat部署负载均衡(或集群)
    WebLogic部署集群和代理服务器
    iOS视频直播初窥:高仿<喵播APP>
    iOS高仿:花田小憩3.0.1
    Preprocessor Macros
    App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. 解决方案
  • 原文地址:https://www.cnblogs.com/xhan/p/1524035.html
Copyright © 2011-2022 走看看