2013-08-02 49 views
4

我知道有很多人回覆此話題,但我對這個回信中示例代碼不工作的每一個.dll文件合併C#DLL的成.EXE

我以前this例子。

public App() 
    { 
     AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly); 
    } 

    static Assembly ResolveAssembly(object sender, ResolveEventArgs args) 
    { 
     //We dont' care about System Assemblies and so on... 
     if (!args.Name.ToLower().StartsWith("wpfcontrol")) 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; 
    } 

當我創建了一個小的程序,只有一個窗口和一個比唐它有工作,但我的「大」 DLL它說好的工作。 「big」dll的設置與我的小程序中的設置相同。

我無法想象它爲什麼有時有效,有時它不工作。我也用ICSharp.AvalonEdit.dll測試過,不成功。

任何人都可以想象錯誤在哪裏?

編輯1

當我開始我的程序它說,它不能找到我的DLL。

編輯2

我想我得到了我的問題的核心。如果其中一個dll的,我想要合併包含對其他dll的引用,則會發生這種情況,例如:FileNotFoundException。難道有人知道如何加載當我使用從吉日波拉塞克代碼它適用於一些/還加了內部所需的dll的

編輯3

。我Fluent顯示錯誤「請附上與風格的ResourceDictionary但我不得不在已經這樣做了我的App.xaml

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Office2010/Silver.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

回答

1

如果您引用的程序集需要其他組件,你有你的應用程序,包括他們還有 - 要麼在應用程序文件夾或嵌入的資源,你可以使用Visual Studio,IL間諜,dotPeek確定引用的程序集,或者使用方法Assembly.GetReferencedAssemblies寫你自己的工具。

這也有可能是你重視ResolveAssembly處理程序之前AssemblyResolve事件被觸發。附加處理程序應該是你在你的Main方法中做的第一件事OD。在WPF中,您必須使用新的Main方法創建新類,並將其設置爲項目設置中的啓動對象

public class Program 
{ 
    [STAThreadAttribute] 
    public static void Main() 
    { 
     AppDomain.CurrentDomain.AssemblyResolve 
      += new ResolveEventHandler(ResolveAssembly); 
     WpfApplication1.App app = new WpfApplication1.App(); 
     app.InitializeComponent(); 
     app.Run(); 
    } 

    public static Assembly ResolveAssembly(object sender, ResolveEventArgs args) 
    { 
     // check condition on the top of your original implementation 
    } 
} 

您也應該檢查的ResolveAssembly頂護罩狀態時不排除任何引用的程序集。

+0

使用你的代碼後,我遇到了一些麻煩。請顯示[這裏](http://stackoverflow.com/questions/18328483/problems-after-merging-fluent-into-main-exe) –