Skip to content

Commit 4c28dc2

Browse files
committed
Dependency resolver added
1 parent 87c1ae2 commit 4c28dc2

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

Core.Common.Standard/DataAccess/DirectoryHelper.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public DirectoryInfo[] GetDirectoryInfos(string path, string searchPattern = nul
3131
{
3232
path = this.pathHelper.ToAbsolute(path);
3333
DirectoryInfo directory = new DirectoryInfo(path);
34+
if (!directory.Exists)
35+
{
36+
return new DirectoryInfo[0];
37+
}
3438
return searchPattern == null ? directory.GetDirectories() : directory.GetDirectories(searchPattern, option);
3539
}
3640

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Text;
2+
3+
namespace KY.Core.Extension
4+
{
5+
public static class StringBuilderExtension
6+
{
7+
public static StringBuilder TrimEnd(this StringBuilder builder)
8+
{
9+
if (builder == null || builder.Length == 0)
10+
{
11+
return builder;
12+
}
13+
int index = builder.Length - 1;
14+
for (; index >= 0; index--)
15+
{
16+
if (!char.IsWhiteSpace(builder[index]))
17+
{
18+
break;
19+
}
20+
}
21+
if (index < builder.Length - 1)
22+
{
23+
builder.Length = index + 1;
24+
}
25+
return builder;
26+
}
27+
}
28+
}

Core.Common.Standard/KY.Core.Common.Standard.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>KY.Core</RootNamespace>
66
<AssemblyName>KY.Core.Common</AssemblyName>
7-
<Version>4.4.0</Version>
7+
<Version>4.5.0</Version>
88
<Authors>KY-Programming</Authors>
99
<Company>KY-Programmingp</Company>
1010
<Product>KY.Core</Product>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text.RegularExpressions;
7+
using KY.Core.DataAccess;
8+
9+
namespace KY.Core
10+
{
11+
public static class NugetPackageDependencyLoader
12+
{
13+
public static void Activate()
14+
{
15+
AppDomain.CurrentDomain.AssemblyResolve += Resolve;
16+
}
17+
18+
public static void Deactivate()
19+
{
20+
AppDomain.CurrentDomain.AssemblyResolve -= Resolve;
21+
}
22+
23+
private static Assembly Resolve(object sender, ResolveEventArgs args)
24+
{
25+
Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.FullName == args.Name);
26+
if (assembly != null)
27+
{
28+
return assembly;
29+
}
30+
Regex regex = new Regex(@"(?<name>[\w.]+),\sVersion=(?<version>[\d.]+),\sCulture=(?<culture>[\w-]+),\sPublicKeyToken=(?<token>\w+)");
31+
Match match = regex.Match(args.Name);
32+
if (match.Success)
33+
{
34+
string name = match.Groups["name"].Value;
35+
if (name.StartsWith("System."))
36+
{
37+
return null;
38+
}
39+
List<DirectoryInfo> versions = FileSystem.GetDirectoryInfos(FileSystem.Combine(Environment.ExpandEnvironmentVariables("%USERPROFILE%"), ".nuget\\packages", name)).ToList();
40+
FileSystem.GetDirectoryInfos(FileSystem.Combine(Environment.ExpandEnvironmentVariables("%PROGRAMFILES%"), "dotnet\\sdk\\NuGetFallbackFolder", name)).ForEach(versions.Add);
41+
Version version = new Version(match.Groups["version"].Value);
42+
DirectoryInfo versionDirectory = versions.FirstOrDefault(x => x.Name == version.ToString())
43+
?? versions.FirstOrDefault(x => x.Name.StartsWith(version.ToString(3)))
44+
?? versions.FirstOrDefault(x => x.Name.StartsWith(version.ToString(2)))
45+
?? versions.FirstOrDefault(x => x.Name.StartsWith(version.ToString(1)));
46+
if (versionDirectory != null)
47+
{
48+
string fullPath = FileSystem.Combine(versionDirectory.FullName, "lib", "netstandard2.0", name) + ".dll";
49+
return Assembly.LoadFile(fullPath);
50+
}
51+
}
52+
return null;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)