private static object Cast(object source, BuiltInType sourceType, BuiltInType targetType)
{
// null always casts to null.
if (source == null)
{
return null;
}
// extract the value from a Variant if specified.
if (source is Variant)
{
return Cast(((Variant)source).Value, targetType);
}
// call the appropriate function if a conversion is supported for the target type.
try
{
switch (targetType)
{
case BuiltInType.Boolean: return ToBoolean(source, sourceType);
case BuiltInType.SByte: return ToSByte(source, sourceType);
case BuiltInType.Byte: return ToByte(source, sourceType);
case BuiltInType.Int16: return ToInt16(source, sourceType);
case BuiltInType.UInt16: return ToUInt16(source, sourceType);
case BuiltInType.Int32: return ToInt32(source, sourceType);
case BuiltInType.UInt32: return ToUInt32(source, sourceType);
case BuiltInType.Int64: return ToInt64(source, sourceType);
case BuiltInType.UInt64: return ToUInt64(source, sourceType);
case BuiltInType.Float: return ToFloat(source, sourceType);
case BuiltInType.Double: return ToDouble(source, sourceType);
case BuiltInType.String: return ToString(source, sourceType);
case BuiltInType.DateTime: return ToDateTime(source, sourceType);
case BuiltInType.Guid: return ToGuid(source, sourceType);
case BuiltInType.ByteString: return ToByteString(source, sourceType);
case BuiltInType.NodeId: return ToNodeId(source, sourceType);
case BuiltInType.ExpandedNodeId: return ToExpandedNodeId(source, sourceType);
case BuiltInType.StatusCode: return ToStatusCode(source, sourceType);
case BuiltInType.QualifiedName: return ToQualifiedName(source, sourceType);
case BuiltInType.LocalizedText: return ToLocalizedText(source, sourceType);
}
}
catch (Exception e)
{
Utils.Trace(e, "Error converting a {1} (Value={0}) to {2}.", source, sourceType, targetType);
}
// conversion not supported.
return null;
}
#endregion