c#, Internal, and Reflection

Posted by cyberconte on Stack Overflow See other posts from Stack Overflow or by cyberconte
Published on 2009-05-02T05:05:37Z Indexed on 2010/05/23 12:30 UTC
Read the original article Hit count: 264

Filed under:
|
|

Duplicate of:
Accessing internal members via System.Reflection?


Is there a way to execute "internal" code via reflection?

Here is an example program:

using System;
using System.Reflection;

namespace ReflectionInternalTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly asm = Assembly.GetExecutingAssembly();

            // Call normally
            new TestClass();

            // Call with Reflection
            asm.CreateInstance("ReflectionInternalTest.TestClass", 
                false, 
                BindingFlags.Default | BindingFlags.CreateInstance, 
                null, 
                null, 
                null, 
                null);

            // Pause
            Console.ReadLine();
        }
    }

    class TestClass
    {
        internal TestClass()
        {
            Console.WriteLine("Test class instantiated");
        }
    }
}

Creating a testclass normally works perfectly, however when i try to create an instance via reflection, I get a missingMethodException error saying it can't find the Constructor (which is what would happen if you tried calling it from outside the assembly).

Is this impossible, or is there some workaround i can do?

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection