有沒有辦法通過反射來執行「內部」代碼?c#,內部和反射
下面是一個例子程序:
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");
}
}
}
創建TestClass的正常工作完美,但是當我嘗試創建通過反射一個實例,我得到一個錯誤missingMethodException說,它不能找到構造函數(其如果你嘗試從組件外部調用它會發生什麼)。
這是不可能的,還是有一些解決方法,我可以做?
只是一個快速一絲AccessPrivateWrapper動態包裝,你可以使用Types.EmptyTypes,而不是新類型[0]。 – Vinicius 2013-02-08 18:51:21