Example #1
0
        protected override int OnMarshalParameter(int i, RpcMethodParameterCollection inParameters, IList <IDataType> inParameterTypes, object[] parameterValues, byte[] buffer, int offset)
        {
            ITcAdsDataType type       = (ITcAdsDataType)inParameterTypes[i];
            object         val        = parameterValues[i];
            AdsDatatypeId  dataTypeId = type.DataTypeId;
            int            num        = 0;

            if (PrimitiveTypeConverter.CanMarshal(type.Category))
            {
                byte[] data = null;
                PrimitiveTypeConverter.Marshal(dataTypeId, val, out data);
                num = copyHelper(buffer, offset, data);
            }
            else
            {
                if (type.Category != DataTypeCategory.String)
                {
                    throw new MarshalException($"Cannot marshal complex type '{type.Name}'");
                }
                int  length    = -1;
                bool isUnicode = false;
                DataTypeStringParser.TryParseString(type.Name, out length, out isUnicode);
                string             str       = (string)val;
                byte[]             source    = null;
                PlcStringConverter converter = isUnicode ? PlcStringConverter.UnicodeVariableLength : PlcStringConverter.DefaultVariableLength;
                if (converter.MarshalSize(str) > type.ByteSize)
                {
                    throw new MarshalException("String size mismatch");
                }
                source = converter.Marshal(str);
                num    = copyHelper(buffer, offset, source);
            }
            return(num);
        }