Skip to content

Commit 3d3261e

Browse files
authored
Merge pull request #369 from 0xced/nrt-safety
Add more nullable type reference safeties
2 parents 79ecfe6 + 5a91b7d commit 3d3261e

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

src/Serilog.Settings.Configuration/Settings/Configuration/Assemblies/DllScanningAssemblyFinder.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ public override IReadOnlyList<AssemblyName> FindAssembliesContainingName(string
3232
}
3333
else
3434
{
35-
probeDirs.Add(Path.GetDirectoryName(typeof(AssemblyFinder).Assembly.Location));
35+
var assemblyLocation = Path.GetDirectoryName(typeof(AssemblyFinder).Assembly.Location);
36+
if (assemblyLocation != null)
37+
{
38+
probeDirs.Add(assemblyLocation);
39+
}
3640
}
3741

3842
var query = from probeDir in probeDirs

src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,12 +484,12 @@ static bool HasImplicitValueWhenNotSpecified(ParameterInfo paramInfo)
484484
return selectedMethod;
485485
}
486486

487-
static bool ParameterNameMatches(string actualParameterName, string suppliedName)
487+
static bool ParameterNameMatches(string? actualParameterName, string suppliedName)
488488
{
489489
return suppliedName.Equals(actualParameterName, StringComparison.OrdinalIgnoreCase);
490490
}
491491

492-
static bool ParameterNameMatches(string actualParameterName, IEnumerable<string> suppliedNames)
492+
static bool ParameterNameMatches(string? actualParameterName, IEnumerable<string> suppliedNames)
493493
{
494494
return suppliedNames.Any(s => ParameterNameMatches(actualParameterName, s));
495495
}

src/Serilog.Settings.Configuration/Settings/Configuration/LoggingFilterSwitchProxy.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class LoggingFilterSwitchProxy
1515
_setProxy = (Action<string?>)Delegate.CreateDelegate(
1616
typeof(Action<string?>),
1717
realSwitch,
18-
expressionProperty.GetSetMethod());
18+
expressionProperty.GetSetMethod() ?? throw new MissingMethodException(type.FullName, "set_Expression"));
1919

2020
_getProxy = (Func<string?>)Delegate.CreateDelegate(
2121
typeof(Func<string?>),
2222
realSwitch,
23-
expressionProperty.GetGetMethod());
23+
expressionProperty.GetGetMethod() ?? throw new MissingMethodException(type.FullName, "get_Expression"));
2424
}
2525

2626
public object RealSwitch { get; }
@@ -42,6 +42,7 @@ public string? Expression
4242
return null;
4343
}
4444

45-
return new LoggingFilterSwitchProxy(Activator.CreateInstance(filterSwitchType, expression));
45+
var realSwitch = Activator.CreateInstance(filterSwitchType, expression) ?? throw new InvalidOperationException($"Activator.CreateInstance returned null for {filterSwitchType}");
46+
return new LoggingFilterSwitchProxy(realSwitch);
4647
}
4748
}

src/Serilog.Settings.Configuration/Settings/Configuration/ObjectArgumentValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ bool TryCreateContainer([NotNullWhen(true)] out object? result)
8585
return false;
8686

8787
var configurationElements = _section.GetChildren().ToArray();
88-
result = Activator.CreateInstance(toType);
88+
result = Activator.CreateInstance(toType) ?? throw new InvalidOperationException($"Activator.CreateInstance returned null for {toType}");
8989

9090
for (int i = 0; i < configurationElements.Length; ++i)
9191
{
@@ -137,7 +137,7 @@ internal static bool TryBuildCtorExpression(
137137
var ctor =
138138
(from c in type.GetConstructors()
139139
from p in c.GetParameters()
140-
let argumentBindResult = suppliedArguments.TryGetValue(p.Name, out var argValue) switch
140+
let argumentBindResult = suppliedArguments.TryGetValue(p.Name ?? "", out var argValue) switch
141141
{
142142
true => new { success = true, hasMatch = true, value = (object?)argValue },
143143
false => p.HasDefaultValue switch

src/Serilog.Settings.Configuration/Settings/Configuration/StringArgumentValue.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public StringArgumentValue(string providedValue)
2121
{
2222
{ typeof(Uri), s => new Uri(s) },
2323
{ typeof(TimeSpan), s => TimeSpan.Parse(s) },
24-
{ typeof(Type), s => Type.GetType(s, throwOnError:true) },
24+
{ typeof(Type), s => Type.GetType(s, throwOnError:true)! },
2525
};
2626

2727
public object? ConvertTo(Type toType, ResolutionContext resolutionContext)
@@ -68,7 +68,7 @@ public StringArgumentValue(string providedValue)
6868
if (toType != typeof(string) &&
6969
TryParseStaticMemberAccessor(argumentValue, out var accessorTypeName, out var memberName))
7070
{
71-
var accessorType = Type.GetType(accessorTypeName, throwOnError: true);
71+
var accessorType = Type.GetType(accessorTypeName, throwOnError: true)!;
7272

7373
// if delegate, look for a method and then construct a delegate
7474
if (typeof(Delegate).IsAssignableFrom(toType) || typeof(MethodInfo) == toType)
@@ -109,9 +109,7 @@ public StringArgumentValue(string providedValue)
109109
// is there a public static property with that name ?
110110
var publicStaticPropertyInfo = accessorType.GetTypeInfo().DeclaredProperties
111111
.Where(x => x.Name == memberName)
112-
.Where(x => x.GetMethod != null)
113-
.Where(x => x.GetMethod.IsPublic)
114-
.FirstOrDefault(x => x.GetMethod.IsStatic);
112+
.FirstOrDefault(x => x.GetMethod != null && x.GetMethod.IsPublic && x.GetMethod.IsStatic);
115113

116114
if (publicStaticPropertyInfo != null)
117115
{

0 commit comments

Comments
 (0)