namespace ZB.MOM.WW.LmxOpcUa.Client.Shared.Helpers;
///
/// Converts raw string values into typed values based on the current value's runtime type.
/// Ported from the CLI tool's OpcUaHelper.ConvertValue.
///
public static class ValueConverter
{
///
/// Converts a raw string value into the runtime type expected by the target node.
///
/// The raw string supplied by the user.
/// The current node value used to infer the target type. May be null.
/// A typed value suitable for an OPC UA write request.
public static object ConvertValue(string rawValue, object? currentValue)
{
return currentValue switch
{
bool => bool.Parse(rawValue),
byte => byte.Parse(rawValue),
short => short.Parse(rawValue),
ushort => ushort.Parse(rawValue),
int => int.Parse(rawValue),
uint => uint.Parse(rawValue),
long => long.Parse(rawValue),
ulong => ulong.Parse(rawValue),
float => float.Parse(rawValue),
double => double.Parse(rawValue),
_ => rawValue
};
}
}