c#SetCompatibleTextRenderingDefault必须在第一个之前调用

Hus*_*ter 6 c#

我试图搜索此异常,但我找不到任何解决方案

我使用下面的代码来调用.NET应用程序:

        Assembly assem = Assembly.Load(Data);
        MethodInfo method = assem.EntryPoint;           
        var o = Activator.CreateInstance(method.DeclaringType);            
        method.Invoke(o, null);
Run Code Online (Sandbox Code Playgroud)

将被调用的应用程序具有一个Form并位于应用程序的EntryPoint中:

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false); //Exception
        Application.Run(new Form1());
    }
Run Code Online (Sandbox Code Playgroud)

SetCompatibleTextRenderingDefault必须IWin32Window在应用程序中创建第一个对象之前调用.

编辑:

        Assembly a = Assembly.Load(Data);
        MethodInfo method = a.GetType().GetMethod("Start");
        var o = Activator.CreateInstance(method.DeclaringType);            
        method.Invoke(o, null);
Run Code Online (Sandbox Code Playgroud)

Jer*_*gen 7

您应该创建一个新方法,跳过初始化并查看Start方法的反射.但是Application.Start会阻止当前的线程.如果您不想启动新的消息泵,则应尝试使用反射查找Form类.


class Program
{
    static void Main(string[] args)
    {
        var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        var filename = Path.Combine(path, "WindowsFormsApplication1.exe");
        var assembly = Assembly.LoadFile(filename);
        var programType = assembly.GetTypes().FirstOrDefault(c => c.Name == "Program"); // <-- if you don't know the full namespace and when it is unique.
        var method = programType.GetMethod("Start", BindingFlags.Public | BindingFlags.Static);
        method.Invoke(null, new object[] { });
    }
}
Run Code Online (Sandbox Code Playgroud)

和装载组件:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Start();
    }


    public static void Start()   // <-- must be marked public!
    {
        MessageBox.Show("Start");
        Application.Run(new Form1());
    }
}
Run Code Online (Sandbox Code Playgroud)

这在这里工作!