I'm using Visual Studio 2017 (15.9.7) Targeting .Net Framework 4.7.1.
I ran into an issue of not having transitive references copied to my output folder.
The code compiles without issues.
I have investigated the issue and found the root cause to be a missing reference in the compiled assembly.
I have distilled down the problem, and here's what I found:
I have 2 class libraries:
- ClassLibrary1 has a xaml UserControl that references a BitmapImage from ClassLibrary2:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:media="clr-namespace:ClassLibrary2;assembly=ClassLibrary2"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Image Source="{x:Static media:MediaResourceImages.Autofocus_16}" />
</UserControl>
- ClassLibrary2 has a png resource file compiled within it, and a public static readonly BitmapImage field in a public static class that's created from the png:
using System;
using System.Windows.Media.Imaging;
namespace ClassLibrary2
{
public static class MediaResourceUris
{
public const string Autofocus_16 = "pack://application:,,,/ClassLibrary2;component/Resources/Autofocus_16.png";
}
public static class MediaResourceImages
{
public static readonly BitmapImage Autofocus_16 = new BitmapImage(new Uri(MediaResourceUris.Autofocus_16));
}
}
After compiling successfully, ILSpy shows that ClassLibrary1 has no reference to ClassLibrary2, even though the baml references a field from ClassLibrary2:
Minimal solution to reproduce via GitHub
Is this normal?
Is this a bug in the compiler?
There's obviously a runtime dependency of ClassLibary1 on ClassLibary2, so why isn't a reference compiled into the metadata of ClassLibrary1?