Embedded resource Lecture: Dll embedded in WPF

First, the new library a:

namespace a
{
    public class Class1
    {
        public string call()
        { return "Hello world!"; }
    }
}

Generating a.dll backup release version.

Second, the new WPF application b:
Add a reference a.dll and copied to the project a.dll b, amended as embedded resources,

modify App.xaml, delete StartupUri where = "MainWindow.xaml",
modify App. xaml.cs, rewrite OnStartup:

using System.Reflection;

namespace
b { /// <Summary> /// interaction logic of App.xaml /// </ Summary> public partial class the App: the Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { String projectName = Assembly.GetExecutingAssembly().GetName().Name.ToString(); using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(projectName + ".a.dll")) { Byte[] b = new Byte[stream.Length]; stream.Read(b, 0, b.Length); return Assembly.Load(b); } }; MainWindow m = new MainWindow(); m.Show(); } } }

Modify MainWindow.xaml.cs:

namespace b
{
    ///  <Summary> 
    /// interaction logic of MainWindow.xaml
     ///  </ Summary> 
    public  partial  class the MainWindow: the Window
    {
        public MainWindow()
        {
            InitializeComponent();

            a.Class1 o = new a.Class1();
            MessageBox.Show(o.call());
        }
    }
}

Generation, b.exe copied to the desktop (make sure no visible a.dll), run it, you're done. (Author: Sword)




Reproduced in: https: //www.cnblogs.com/aswordok/p/3735919.html

Guess you like

Origin blog.csdn.net/weixin_33725807/article/details/93726746