private bool CheckNull <T>(ref ParamTuple tup, Func <T, T> drawMethod) { if (typeof(T) == tup.ParamInfo.ParameterType) { if (DBNull.Value.Equals(tup.Obj)) { tup.Obj = default(T); } tup.Obj = drawMethod((T)tup.Obj); return(true); } return(false); }
public static object DeserializeResponseValue(ResponseMessage response, Type returnType) { if (response == null) { return(null); } if (returnType == typeof(void)) { return(null); } if (ReflectionUtils.IsCollectionType(returnType)) { var items = DeserializeArray(response.ReturnValue, returnType); if (returnType.IsArray) { // not strongly typed array if (returnType.GetElementType() == typeof(object)) { return(items); } return(ReflectionUtils.MakeStronglyTypedArray(returnType.GetElementType(), items)); } // Some other type of collection var itemType = returnType.GetGenericArguments()[0]; return(ReflectionUtils.MakeStronglyTypedEnumerable(itemType, items)); } if (response.ReturnValue == null) { return(null); } var paramTuple = new ParamTuple(response.ReturnValue, response.ReturnValueType); return(DeserializeParamTuple(paramTuple, returnType)); }
public static ParamTuple[] ParametersToParamTuple(object[] arguments, Type[] parametersTypes) { if (arguments == null) { return(null); } if (parametersTypes.Length == 0) { return(null); } // Not using linq for perf reasons var retArray = new ParamTuple[parametersTypes.Length]; for (int i = 0; i < retArray.Length; i++) { // var pTuple = Serialization.BuildParamTuple(parametersTypes[i], arguments[i]); var pTuple = Serialization.BuildParamTuple(arguments[i].GetType(), arguments[i]); retArray[i] = pTuple; } return(retArray); }
public static object DeserializeParamTuple(ParamTuple paramTuple, Type expectedType) { if (paramTuple == null) { return(null); } // should we cache this lookup? var envelopedType = paramTuple.TypeName == "string" ? typeof(string) : Type.GetType(paramTuple.TypeName, true); var res = ProtoBuf.Meta.RuntimeTypeModel.Default.Deserialize( new MemoryStream(paramTuple.SerializedValue), null, envelopedType); if (expectedType == envelopedType) // best case scenario { return(res); } if (expectedType == typeof(Int16)) { return(Convert.ToInt16((string)res)); } if (expectedType == typeof(Int32)) { return(Convert.ToInt32((string)res)); } if (expectedType == typeof(Int64)) { return(Convert.ToInt64((string)res)); } if (expectedType == typeof(UInt16)) { return(Convert.ToUInt16((string)res)); } if (expectedType == typeof(UInt32)) { return(Convert.ToUInt32((string)res)); } if (expectedType == typeof(UInt64)) { return(Convert.ToUInt64((string)res)); } if (expectedType == typeof(decimal)) { return(Convert.ToDecimal((string)res)); } if (expectedType == typeof(Single)) { return(Convert.ToSingle((string)res)); } if (expectedType == typeof(Double)) { return(Convert.ToDouble((string)res)); } if (expectedType == typeof(Guid)) { return(Guid.Parse((string)res)); } if (expectedType == typeof(Byte)) { return(Convert.ToByte((string)res)); } if (expectedType == typeof(SByte)) { return(Convert.ToSByte((string)res)); } if (expectedType == typeof(bool)) { return(Convert.ToBoolean((string)res)); } if (expectedType.IsEnum) { return(Enum.Parse(expectedType, (string)res)); } if (expectedType == typeof(DateTime)) { var parts = res.ToString().Split(new [] { '|' }); var ticks = Convert.ToInt64(parts[0]); var kind = Convert.ToInt32(parts[1]); return(new DateTime(ticks, (DateTimeKind)kind)); } // not special case, got to be something known to protobuf (has a contract) return(res); }