Closed TimLovellSmith closed 2 years ago
Quick research into CopyDefaultValueConstant makes it look like this might because of this kind of edge case:
Could you please provide a minimal code example that reproduces the reported exception? (And yes, please do test it against the latest published version.)
/ping @TimLovellSmith. The information you gave so far is insufficient for us to reproduce the reported problem. Please provide a minimal but complete repro code example; otherwise, I'm going to close this issue in a few days' time.
Closing due to missing repro code & inactivity. @TimLovellSmith, we can reopen once we have something that allows us to reproduce this error.
Here, is the kind of code I was trying to fake. I produced it with Castle 4.3.1, looks like it may be fixed since then.
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace RedisRP.Handlers
{
public interface IAuthorizeRequests
{
Task<(bool, HttpResponseMessage)> IsAllowedAsync(HttpRequestMessage request, CancellationToken token = default);
}
}
Ha... with decompile to source code, I see I can still 'repro' it, but it wasn't really a bug at all. Unless you are trying to avoid first-chance exceptions.
private void CopyDefaultValueConstant(ParameterInfo from, ParameterBuilder to)
{
object obj;
try
{
obj = from.DefaultValue;
}
catch (FormatException) when (from.ParameterType == typeof(DateTime))
{
obj = null;
}
catch (FormatException) when (from.ParameterType.IsEnum)
{
obj = null;
}
if (obj is Missing)
{
return;
}
try
{
to.SetConstant(obj); // this line throws
So yes, this was always good to close.
Ah, yes. Thanks for letting us know.
In case you're wondering why we need to catch exceptions there, check out the code comments in MethodEmitter.cs
. (In short, it is to work around various bugs and limitations in the CLR.)
[Sorry if this is an obsolete bug, haven't tried to repro with latest yet.]
Packages.config:
<package id="Castle.Core" version="4.3.1" targetFramework="net472" />
Observed: managed to get this first-chance exception
{"Null is not a valid constant value for this type."} | System.ArgumentException
as a first-chance exception.Expected behavior: this seems like a bit of unnecessary exception-oriented programming where conditional tests, or appropriate method overloading, would be better and faster.
Local variable
from
refers to the cancellation token parameter inCopyDefaultValueConstant()
.{Name = "CancellationToken" FullName = "System.Threading.CancellationToken"}