一、新建类库a:
namespace a { public class Class1 { public string call() { return "Hello world!"; } } }
生成release版的a.dll备用。
二、新建WPF应用程序b:
添加a.dll的引用,并复制a.dll到项目b,修改为嵌入的资源,
修改App.xaml,删掉其中的StartupUri="MainWindow.xaml",
修改App.xaml.cs,改写OnStartup:
using System.Reflection;
namespace b { /// <summary> /// App.xaml 的交互逻辑 /// </summary> public partial class App : 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(); } } }
修改MainWindow.xaml.cs:
namespace b { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); a.Class1 o = new a.Class1(); MessageBox.Show(o.call()); } } }
生成,复制b.exe到桌面(确保没有可见的a.dll),运行之,大功告成。(作者:一剑)
转载于:https://www.cnblogs.com/aswordok/p/3735919.html