Accessing internal members via System.Reflection?

后端 未结 5 862
一个人的身影
一个人的身影 2020-12-08 11:18

I\'m trying to Unit Test a class that has many internal functions. These obviously need testing too, but my Tests project is seperate, mainly because it covers many small, r

5条回答
  •  轮回少年
    2020-12-08 11:24

    It would be more appropriate to use the InternalsVisibleTo attribute to grant access to the internal members of the assembly to your unit test assembly.

    Here is a link with some helpful additional info and a walk through:

    • The Wonders Of InternalsVisibleTo

    To actually answer your question... Internal and protected are not recognized in the .NET Reflection API. Here is a quotation from MSDN:

    The C# keywords protected and internal have no meaning in IL and are not used in the Reflection APIs. The corresponding terms in IL are Family and Assembly. To identify an internal method using Reflection, use the IsAssembly property. To identify a protected internal method, use the IsFamilyOrAssembly.

提交回复
热议问题