在C#中使用反射调用internal的方法

MSDN上解释Internal如下:

The internal keyword is an access modifier for types and type members. Internal types or members are accessible only within files in the same assembly.

即, 仅允许相同程序集内的代码调用类型或成员.

 

那么是否可以调用这些internal的方法呢?

如果被调用的程序集, 在代码中使用了InternalsVisibleToAttribute来标示一个或多个友元程序集, 那么这些被标为友元的程序集就可以访问被调用程序集的internal方法. 下例是程序集A的代码, 它宣布AssemblyB为友元程序集

// This file is for Assembly A.

using System.Runtime.CompilerServices;
using System;

[assembly: InternalsVisibleTo("AssemblyB")]

// The class is internal by default.
class FriendClass
{
    public void Test()
    {
        Console.WriteLine("Sample Class");
    }
}

// Public class that has an internal method.
public class ClassWithFriendMethod
{
    internal void Test()
    {
        Console.WriteLine("Sample Method");
    }

}

 

更具体的一行代码示例如下:

[assembly: InternalsVisibleTo("AssemblyB, PublicKey=32ab4ba45e0a69a1")]

 

那么如果我们要调用的是第三方人写的代码里的internal的方法, 怎么办呢?

答案是使用反射.

 

下面是被调用的类的源代码.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace internalclasstest
{
    public class PubClass
    {
        public void Speak()
        {
            Console.WriteLine("PubClass speaks: You are so nice!");
        }

        //Internal method
        internal void Mock()
        {
            Console.WriteLine("PubClass mocks: You suck!");
        }
    }

    //Internal class
    class InternalClass
    {
        public void Speak()
        {
            Console.WriteLine("InternalClass speaks: I love my job!");
        }

        void Moci()
        {
            Console.WriteLine("InternalClass speaks: I love Friday night!");
        }
    }
}

 

下面是使用反射并调用PubClass的Internal 函数Mock的代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace reflectionInternal
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly asm = Assembly.LoadFile(@"E:\internalclasstest\bin\Debug\internalclasstest.dll");
            Type t1 = asm.GetType("internalclasstest.PubClass");
            
            ConstructorInfo t1Constructor = t1.GetConstructor(Type.EmptyTypes);
            Object oPubClass = t1Constructor.Invoke(new Object[] { });

            MethodInfo oMethod = t1.GetMethod("Mock", BindingFlags.Instance | BindingFlags.NonPublic);
            oMethod.Invoke(oPubClass, new Object[]{});
        }
    }
}

 

 

资料来源:

internal (C# Reference)

http://msdn.microsoft.com/en-us/library/7c5ka91b%28VS.80%29.aspx 

Friend Assemblies (C# and Visual Basic)

http://msdn.microsoft.com/en-us/library/0tke9fxk.aspx

Accessing internal members via System.Reflection?

http://stackoverflow.com/questions/171332/accessing-internal-members-via-system-reflection

Using reflection to call an internal method

http://www.codeproject.com/Messages/1635613/Using-reflection-to-call-an-internal-method.aspx

MethodBase.Invoke Method (Object, Object[])

http://msdn.microsoft.com/en-us/library/a89hcwhh.aspx

BindingFlags Enumeration

http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx

posted on   中道学友  阅读(10498)  评论(1编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律

导航

< 2010年5月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

技术追求准确,态度积极向上

点击右上角即可分享
微信分享提示