Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more nullable type reference safeties #369

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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