private object ConvertRemoteToLocal(object srcValue, BuiltInType srcType, BuiltInType dstType)
{
// check for null.
if (srcValue == null)
{
return null;
}
// must determine the type from the source if the containing array is a variant.
if (srcType == BuiltInType.Variant)
{
TypeInfo typeInfo = TypeInfo.Construct(srcValue);
srcType = typeInfo.BuiltInType;
if (typeInfo.ValueRank != ValueRanks.Scalar)
{
return TypeInfo.CastArray((Array)srcValue, srcType, BuiltInType.Null, ConvertRemoteToLocal);
}
}
// no conversion by default.
object dstValue = srcValue;
// apply different conversions depending on the data type.
switch (srcType)
{
case BuiltInType.Guid:
{
dstValue = ((Uuid)srcValue).ToString();
break;
}
case BuiltInType.XmlElement:
{
dstValue = ((XmlElement)srcValue).OuterXml;
break;
}
case BuiltInType.NodeId:
{
dstValue = GetLocalItemId((NodeId)srcValue);
break;
}
case BuiltInType.ExpandedNodeId:
{
ExpandedNodeId nodeId = GetLocaleExpandedNodeId((ExpandedNodeId)srcValue);
dstValue = nodeId.ToString();
break;
}
case BuiltInType.QualifiedName:
{
dstValue = GetLocalBrowseName((QualifiedName)srcValue);
break;
}
case BuiltInType.LocalizedText:
{
dstValue = ((LocalizedText)srcValue).Text;
break;
}
case BuiltInType.StatusCode:
{
dstValue = ((StatusCode)srcValue).Code;
break;
}
case BuiltInType.ExtensionObject:
{
BinaryEncoder encoder = new BinaryEncoder(m_localMessageContext);
encoder.WriteExtensionObject(null, (ExtensionObject)srcValue);
dstValue = encoder.CloseAndReturnBuffer();
break;
}
case BuiltInType.Variant:
{
dstValue = ((Variant)srcValue).Value;
break;
}
default:
{
if (dstType != srcType && dstType != BuiltInType.Variant && dstType != BuiltInType.Null)
{
throw ComUtils.CreateComException(ResultIds.E_BADTYPE);
}
break;
}
}
// all done.
return dstValue;
}