Sicherung
This commit is contained in:
39
Config.Net/TypeParsers/ByteParser.cs
Normal file
39
Config.Net/TypeParsers/ByteParser.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace Config.Net.TypeParsers
|
||||
{
|
||||
class ByteParser : ITypeParser
|
||||
{
|
||||
public IEnumerable<Type> SupportedTypes => new[] { typeof(byte) };
|
||||
|
||||
public bool TryParse(string? value, Type t, out object? result)
|
||||
{
|
||||
if(value == null)
|
||||
{
|
||||
result = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
byte ir;
|
||||
bool parsed;
|
||||
if (value.StartsWith("0x"))
|
||||
{
|
||||
parsed = byte.TryParse(value.Substring(2), NumberStyles.HexNumber, TypeParserSettings.DefaultCulture, out ir);
|
||||
}
|
||||
else
|
||||
{
|
||||
parsed = byte.TryParse(value, NumberStyles.Integer, TypeParserSettings.DefaultCulture, out ir);
|
||||
}
|
||||
result = ir;
|
||||
return parsed;
|
||||
}
|
||||
|
||||
public string? ToRawString(object? value)
|
||||
{
|
||||
return ((byte?)value)?.ToString(TypeParserSettings.DefaultNumericFormat, TypeParserSettings.DefaultCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user