从引用Core

时间:2019-01-22 07:55:33

标签: wpf reflection .net-assembly

我有一个项目,它指向另一个项目,一个核心。在第三个项目中,我正在解析装配。我很容易就能获得第一个项目,但是由于核心原因,它给我一个错误,找不到它。你知道要添加什么吗?

         public MainWindow()
    {
        InitializeComponent();
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {

        var assembly = Assembly.Load(@"C:\Users\source\Project1\...\MainProgramm.exe");
        var windowType = assembly.DefinedTypes.FirstOrDefault(t => t != typeof(Window) && typeof(Window).IsAssignableFrom(t));

        var window = (Window)Activator.CreateInstance(windowType);
        window.Background = Brushes.Green;
        var tmp = window.ShowDialog();
    }


    static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        AssemblyName an = AssemblyName.GetAssemblyName(args.Name);

        using(var memoryStream = new MemoryStream())
        using(var fileStream = new FileStream(args.Name, FileMode.Open))
        {
            byte[] bytes = new byte[fileStream.Length];
            fileStream.Read(bytes, 0, (int)fileStream.Length);
            memoryStream.Write(bytes, 0, (int)fileStream.Length);
            return Assembly.Load(bytes);
        }
    }

“ CoreProject”在文件夹路径中:@"C:\Users\source\Project1\Project1Core\"

0 个答案:

没有答案