程序集依赖问题

时间:2020-06-11 10:42:38

标签: c# wpf dll .net-assembly

情况:

我有树项目(ZShared,ZSearcher和ZClient),它们将在彼此之间被引用。

ZShared是一个常见的DLL程序集,其中包含一些样式和资源。

ZSearcher还是具有某些WPF控件的DLL程序集。

从理论上讲,ZClient可以是用于测试目的的任何东西(WPF应用程序,Winforms,Excel等)。

问题:

当我在ZSearcher中引用ZShared时,它会生成两个DLL文件:ZShared.DLL和ZSearcher.DLL

在ZClient中引用ZSearcher时,仅ZSearcher被复制到ZClient文件夹。也可以通过引用ZShared来解决。

但是我希望ZSearcher可以作为独立应用程序使用。就像引用ZSearcher一样,依赖关系也将自动跟随。

因此,我认为也许使用反射而不是引用将解决此问题。但是反射会发生完全相同的问题。

System.Windows.Markup.XamlParseException HResult = 0x80131501
消息='设置属性'System.Windows.ResourceDictionary.Source' 例外。”行号“ 10”和行位置“ 18”。
来源= PresentationFramework
StackTrace:位于 System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory,布尔值 skipJournaledProperties,对象rootObject,XamlObjectWriterSettings 设置,Uri baseUri)位于 System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, 布尔型skipJournaledProperties,对象rootObject,XamlAccessLevel accessLevel,Uri baseUri)位于 System.Windows.Markup.XamlReader.LoadBaml(流流,ParserContext parserContext,对象父级,布尔型closeStream)位于 System.Windows.Application.LoadComponent(对象组件,Uri ZSearcher.SearcherWindow.InitializeComponent()的resourceLocator) 在 C:\ Users \ nn \ Desktop \ WorkSpaceVS \ TestApplication \ ZSearcher \ SearcherWindow.xaml:line 1

内部异常1:FileNotFoundException:无法加载文件或 程序集“ ZShared,文化=中性”或其依赖项之一。的 系统找不到指定的文件。

问题的重印:

创建一个.NET-Framework C# DLL组装项目(ZShared)。该程序集仅包含一个ResourceDictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <SolidColorBrush x:Key="ZSolidColorBrushRed" Color="Red"/>
    <Style x:Key="ZButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Foreground" Value="Green"/>
    </Style>
</ResourceDictionary>

创建另一个.NET-Framework C# DLL组装项目(ZSearcher)。该程序集包含一个Window和一个Class

Searcher.cs类:

namespace ZSearcher
{
    public static class Searcher
    {
        public static object Search(string param)
        {
            var window = new SearcherWindow();
            window.ShowDialog();

            return null;
        }
    }
}

SearcherWindow.xaml:

<Window x:Class="ZSearcher.SearcherWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Height="300" Width="500"
         Title="ZSearcher">

   <Window.Resources>
       <ResourceDictionary>
           <ResourceDictionary.MergedDictionaries>
               <ResourceDictionary Source="pack://Application:,,,/ZShared;component/ZResources.xaml"/>
           </ResourceDictionary.MergedDictionaries>
       </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <StackPanel Margin="20">
            <TextBlock Text="SolidColorBrush test" Foreground="{DynamicResource ZSolidColorBrushRed}"/>
            <Button Content="Button style test" Style="{DynamicResource ZButtonStyle}"/>
        </StackPanel>
    </Grid>
</Window>

在ZSearcher项目中引用ZShared.DLL。

创建一个.NET-Framework WPF C#应用。此应用仅包含MainWindow。

MainWindow.xaml:

<Window x:Class="ZClient.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ZClient" Width="400" Height="200">
    <Grid>
        <Button Content="Open searcher" Click="OpenSearcher_Click" Width="100" Height="30"/>
    </Grid>
</Window>

MainWindow.cs

using System.Reflection;
using System.Windows;
using ZSearcher;

namespace ZClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OpenSearcher_Click(object sender, RoutedEventArgs e)
        {
            //Referenced tester
            var referenceTest = Searcher.Search("Test");

            //Reflection tester
            var test = Test("Search");
        }

        private static object Test(string methodName)
        {
            var assembly = Assembly.LoadFrom(@"C:\Users\nn\Desktop\WorkSpaceVS\TestApplication\ZSearcher\bin\Debug\ZSearcher.DLL");
            var type = assembly.GetType("ZSearcher.Searcher");

            if (type == null) return null;
            var methodInfo = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static);

            if (methodInfo == null) return null;
            var parametersArray = new object[] { "Test" };

            return methodInfo.Invoke(null, parametersArray);
        }
    }
}

问题:

如何使此ZSearcher程序集独立运行?

1 个答案:

答案 0 :(得分:1)

MSBuild构建解决方案时,它需要在ZSearcher和ZShared之间建立代码级引用,以便正确检测依赖关系并将其复制到ZClient bin文件夹中。

有些人会创建一个虚拟代码引用来解决此问题。

using ZShared;

namespace ZSearcher
{
    public static class Searcher
    {
        static Searcher()
        {
            // Reference something from ZShared here...
        }

        public static object Search(string param)
        {
            var window = new SearcherWindow();
            window.ShowDialog();

            return null;
        }
    }
}