C# mvc4 web api-将uint/int/enum视为long,将float/double视为double
我的任务是使用ASP.NET MVC4 Web API创建RESTfull服务。其中一种服务方法如下所示:C# mvc4 web api-将uint/int/enum视为long,将float/double视为double,c#,asp.net-mvc,json,asp.net-web-api,C#,Asp.net Mvc,Json,Asp.net Web Api,我的任务是使用ASP.NET MVC4 Web API创建RESTfull服务。其中一种服务方法如下所示: [HttpPost] public HttpResponseMessage TagAdd([FromBody] Tag tag) { HttpResponseMessage result; Tag tmpTag = new Tag(); tmpTag.Name = tag.Name; tmpTag.DataType = tag.DataType; } 此处的标签
[HttpPost]
public HttpResponseMessage TagAdd([FromBody] Tag tag)
{
HttpResponseMessage result;
Tag tmpTag = new Tag();
tmpTag.Name = tag.Name;
tmpTag.DataType = tag.DataType;
}
此处的标签如下所示:
public class Tag : Dictionary<Tag.Property, object>
{
public enum Property : int
{
Name = 0,
DataType
}
public enum NativeDataType : int
{
Undefined = 0,
Scaled,
Float,
}
public string Name
{
get
{
object value;
return TryGetValue(Property.Name, out value) ? (string)value : null;
}
set
{
this[Property.Name] = value;
}
}
public NativeDataType DataType
{
get
{
object value;
return TryGetValue(Property.DataType, out value) ? (NativeDataType)(value) : NativeDataType.Undefined;
}
set
{
this[Property.DataType] = value;
}
}
公共类标记:字典
{
公共枚举属性:int
{
Name=0,
数据类型
}
公共枚举NativeDataType:int
{
未定义=0,
缩放,
浮动
}
公共字符串名
{
得到
{
目标价值;
返回TryGetValue(Property.Name,out value)?(string)值:null;
}
设置
{
此[Property.Name]=值;
}
}
公共NativeDataType数据类型
{
得到
{
目标价值;
返回TryGetValue(Property.DataType,out值)?(NativeDataType)(值):NativeDataType.Undefined;
}
设置
{
此[Property.DataType]=值;
}
}
}
这里当前的问题是,当我发送下面的JSON请求时,enum NativeDataType处理的时间与我一样长:
{“名称”:“ABCD”,“数据类型”:2}
不幸的是,标记类是在另一个程序集中定义的。因此
tmpTag.DataType=tag.DataType;由于跨境问题导致异常(无效转换)的语句
但是,与其逐个访问标记元素并将其转换为精确类型,还有什么最简单的方法可以自动转换并发送到另一个程序集吗?不转换它,您可以只执行简单的转换吗?同样,从int继承应该默认为INT32,但由于某些原因,您得到的是int64(long)。尝试显式地从int32继承