2013-08-02 50 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) –