This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathLoadingResourceDictionary.cs
More file actions
78 lines (68 loc) · 2.41 KB
/
Copy pathLoadingResourceDictionary.cs
File metadata and controls
78 lines (68 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.IO;
using System.Windows;
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
#pragma warning disable CA1010 // Collections should implement generic interface
namespace GitHub
{
public class LoadingResourceDictionary : ResourceDictionary
{
static Dictionary<string, Assembly> assemblyDicts = new Dictionary<string, Assembly>();
public new Uri Source
{
get { return base.Source; }
set
{
EnsureAssemblyLoaded(value);
base.Source = value;
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom")]
void EnsureAssemblyLoaded(Uri value)
{
try
{
var assemblyName = FindAssemblyNameFromPackUri(value);
if (assemblyName == null)
{
Trace.WriteLine($"Couldn't find assembly name in '{value}'");
return;
}
var baseDir = Path.GetDirectoryName(GetType().Assembly.Location);
var assemblyFile = Path.Combine(baseDir, assemblyName + ".dll");
if (assemblyDicts.ContainsKey(assemblyFile))
{
return;
}
if (!File.Exists(assemblyFile))
{
Trace.WriteLine($"Couldn't find assembly at '{assemblyFile}'");
return;
}
var assembly = Assembly.LoadFrom(assemblyFile);
assemblyDicts.Add(assemblyFile, assembly);
}
catch (Exception)
{
Trace.WriteLine($"Error loading assembly for '{value}");
}
}
static string FindAssemblyNameFromPackUri(Uri packUri)
{
var path = packUri.LocalPath;
if (!path.StartsWith("/", StringComparison.OrdinalIgnoreCase))
{
return null;
}
var component = ";component/";
int componentIndex = path.IndexOf(component, 1, StringComparison.OrdinalIgnoreCase);
if (componentIndex == -1)
{
return null;
}
return path.Substring(1, componentIndex - 1);
}
}
}