From 653d458ad6de4186108587a4b9d69d1a4fd23e1e Mon Sep 17 00:00:00 2001 From: Bradley Grainger Date: Sun, 5 Nov 2023 11:56:03 -0800 Subject: [PATCH] Fix NullReferenceException on non-Windows platforms. --- Directory.Build.props | 1 + System.Resources.NetStandard/ResXDataNode.cs | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 95d2ed4..b17679f 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -14,5 +14,6 @@ MIT https://github.com/farlee2121/ResXResourceReader.NetStandard ResX;ResXResourceReader;System.Resources;ResXResourceWriter;netstandard + 9.0 \ No newline at end of file diff --git a/System.Resources.NetStandard/ResXDataNode.cs b/System.Resources.NetStandard/ResXDataNode.cs index fa2df1a..70942a4 100644 --- a/System.Resources.NetStandard/ResXDataNode.cs +++ b/System.Resources.NetStandard/ResXDataNode.cs @@ -864,8 +864,8 @@ internal class AssemblyNamesTypeResolutionService : ITypeResolutionService private Hashtable cachedAssemblies; private Hashtable cachedTypes; - private static readonly string s_dotNetPath = Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles"), "dotnet\\shared"); - private static readonly string s_dotNetPathX86 = Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles(x86)"), "dotnet\\shared"); + private static readonly string s_dotNetPath = Environment.GetEnvironmentVariable("ProgramFiles") is { } programFiles ? Path.Combine(programFiles, "dotnet", "shared") : null; + private static readonly string s_dotNetPathX86 = Environment.GetEnvironmentVariable("ProgramFiles(x86)") is { } programFilesX86 ? Path.Combine(programFilesX86, "dotnet", "shared") : null; internal AssemblyNamesTypeResolutionService(AssemblyName[] names) { @@ -1054,7 +1054,7 @@ public Type GetType(string name, bool throwOnError, bool ignoreCase) /// private bool IsDotNetAssembly(string assemblyPath) { - return assemblyPath != null && (assemblyPath.StartsWith(s_dotNetPath, StringComparison.OrdinalIgnoreCase) || assemblyPath.StartsWith(s_dotNetPathX86, StringComparison.OrdinalIgnoreCase)); + return assemblyPath != null && s_dotNetPath is not null && (assemblyPath.StartsWith(s_dotNetPath, StringComparison.OrdinalIgnoreCase) || assemblyPath.StartsWith(s_dotNetPathX86, StringComparison.OrdinalIgnoreCase)); } public void ReferenceAssembly(AssemblyName name)