-
Notifications
You must be signed in to change notification settings - Fork 84
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
DateTime parsing error when the system has customized datetime format #76
Comments
I also encountered the same error, and I found that this error originates from Convert, ultimately tracing back to the dotnet runtime. My temporary solution is as follows:
|
That's brilliant! public static class ArchiveFilePatch
{
private static Type PropNameId = AccessTools.TypeByName("SevenZipExtractor.ItemPropId");
public static void Patch(Harmony harmony)
{
var original = AccessTools.Method(typeof(SevenZipExtractor.ArchiveFile), "GetPropertySafe", new Type[] { typeof(uint), PropNameId }).MakeGenericMethod(typeof(DateTime));
var prefix = AccessTools.Method(typeof(ArchiveFilePatch), "GetPropertySafe");
harmony.Patch(original, new HarmonyMethod(prefix));
}
public static bool GetPropertySafe(uint fileIndex, object name, ref DateTime __result, ref ArchiveFile __instance)
{
try
{
MethodInfo method = __instance.GetType().GetMethod("GetProperty", AccessTools.all).MakeGenericMethod(typeof(DateTime));
if (method == null)
{
__result = default(DateTime);
return false;
} else {
__result = (DateTime)method.Invoke(__instance, new object[] { fileIndex, name });
return false;
}
}
catch (Exception)
{
__result = default(DateTime);
}
return false;
}
} and call the HarmonyLib.Harmony harmony = new HarmonyLib.Harmony("xxxx");
// patch to fix the SevenZipExtractor issue
ArchiveFilePatch.Patch(harmony); |
Env
Win11pro 23H2
dotnet8
How to reproduce
Expected
Extracted successfully.
Actual
System.FormatException was thrown
because in
T result = (T)Convert.ChangeType(value.ToString(), underlyingType);
,value.ToString()
will output "10/25/2024/Friday 18:00:00", while this format cannot be parsed to DateTime by default. thus in methodprivate T GetPropertySafe<T>(uint fileIndex, ItemPropId name)
, onlyInvalidCastException
was handled, butFormatException
was not. it's not safe now XD.The text was updated successfully, but these errors were encountered: