Skip to content

Commit

Permalink
Merge pull request #369 from 0xced/nrt-safety
Browse files Browse the repository at this point in the history
Add more nullable type reference safeties
  • Loading branch information
nblumhardt committed Mar 21, 2023
2 parents 79ecfe6 + 5a91b7d commit 3d3261e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public override IReadOnlyList<AssemblyName> FindAssembliesContainingName(string
}
else
{
probeDirs.Add(Path.GetDirectoryName(typeof(AssemblyFinder).Assembly.Location));
var assemblyLocation = Path.GetDirectoryName(typeof(AssemblyFinder).Assembly.Location);
if (assemblyLocation != null)
{
probeDirs.Add(assemblyLocation);
}
}

var query = from probeDir in probeDirs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,12 +484,12 @@ static bool HasImplicitValueWhenNotSpecified(ParameterInfo paramInfo)
return selectedMethod;
}

static bool ParameterNameMatches(string actualParameterName, string suppliedName)
static bool ParameterNameMatches(string? actualParameterName, string suppliedName)
{
return suppliedName.Equals(actualParameterName, StringComparison.OrdinalIgnoreCase);
}

static bool ParameterNameMatches(string actualParameterName, IEnumerable<string> suppliedNames)
static bool ParameterNameMatches(string? actualParameterName, IEnumerable<string> suppliedNames)
{
return suppliedNames.Any(s => ParameterNameMatches(actualParameterName, s));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class LoggingFilterSwitchProxy
_setProxy = (Action<string?>)Delegate.CreateDelegate(
typeof(Action<string?>),
realSwitch,
expressionProperty.GetSetMethod());
expressionProperty.GetSetMethod() ?? throw new MissingMethodException(type.FullName, "set_Expression"));

_getProxy = (Func<string?>)Delegate.CreateDelegate(
typeof(Func<string?>),
realSwitch,
expressionProperty.GetGetMethod());
expressionProperty.GetGetMethod() ?? throw new MissingMethodException(type.FullName, "get_Expression"));
}

public object RealSwitch { get; }
Expand All @@ -42,6 +42,7 @@ public string? Expression
return null;
}

return new LoggingFilterSwitchProxy(Activator.CreateInstance(filterSwitchType, expression));
var realSwitch = Activator.CreateInstance(filterSwitchType, expression) ?? throw new InvalidOperationException($"Activator.CreateInstance returned null for {filterSwitchType}");
return new LoggingFilterSwitchProxy(realSwitch);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ bool TryCreateContainer([NotNullWhen(true)] out object? result)
return false;

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

for (int i = 0; i < configurationElements.Length; ++i)
{
Expand Down Expand Up @@ -137,7 +137,7 @@ internal static bool TryBuildCtorExpression(
var ctor =
(from c in type.GetConstructors()
from p in c.GetParameters()
let argumentBindResult = suppliedArguments.TryGetValue(p.Name, out var argValue) switch
let argumentBindResult = suppliedArguments.TryGetValue(p.Name ?? "", out var argValue) switch
{
true => new { success = true, hasMatch = true, value = (object?)argValue },
false => p.HasDefaultValue switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public StringArgumentValue(string providedValue)
{
{ typeof(Uri), s => new Uri(s) },
{ typeof(TimeSpan), s => TimeSpan.Parse(s) },
{ typeof(Type), s => Type.GetType(s, throwOnError:true) },
{ typeof(Type), s => Type.GetType(s, throwOnError:true)! },
};

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

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

if (publicStaticPropertyInfo != null)
{
Expand Down

0 comments on commit 3d3261e

Please sign in to comment.