코딩을 하다보면 dll 들이 엄청 늘어 날때가 있다.

폴더가 지저분해진다.

보기 않좋다.

 

그래서 모든 dll 을 Embed 시키는 방법을 알아 본다.

기존의 ilmerge 라는 툴이 있지만 WPF 등과 같은 프레임웤에 사용하려면 관련된 dll 을 다 열거 하라고 하고 사용법 또한 그리 스마트 하지 않다.

AppDomain.CurrentDomain.AssemblyResolve 를 이용하여 하나로 합칠수가 있다.

우선 사용될 모든 dll 을 비주얼 스튜디오 안으로 끌어 온다

 

.

 

그다음 이 모든 dll 들을 포함 리소스로 만들어 준다.

 

그리고 App 클래스로 이동하여 아래와 같이 코딩 한다.

 

 

using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.IO; using System.Linq; using System.Reflection; using System.Threading.Tasks; using System.Windows; namespace Test_Marge { public partial class App : Application { public App() { AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly); }

static Assembly ResolveAssembly(object sender, ResolveEventArgs args) {
//We dont' care about System Assembies and so on...
//if (!args.Name.ToLower().StartsWith("Test")) return null;
Assembly thisAssembly = Assembly.GetExecutingAssembly();
//Get the Name of the AssemblyFile
var name = args.Name.Substring(0, args.Name.IndexOf(',')) + ".dll";
//Load form Embedded Resources - This Function is not called if the Assembly is in the Application Folder
var resources = thisAssembly.GetManifestResourceNames().Where(s => s.EndsWith(name));
if (resources.Count() > 0) {
var resourceName = resources.First();
using (Stream stream = thisAssembly.GetManifestResourceStream(resourceName)) {
if (stream == null) return null;
var block = new byte[stream.Length];
stream.Read(block, 0, block.Length);
return Assembly.Load(block);
}
}
return null;
}
}
}

 

 

그리고 난 다음 빌드된 폴더의 exe 만 따로 꺼내어 테스트 해본다.

 

 

출처 : http://blog.mahop.net/post/Merge-WPF-Assemblies.aspx

 

 

참고 : Winform 에서는 Program.cs 의 Application.Run(new Form1()); 위에 작성한다.
Posted by cyj4369
,