在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

你可能感兴趣的:(intern)