Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# mvc4 web api-将uint/int/enum视为long,将float/double视为double_C#_Asp.net Mvc_Json_Asp.net Web Api - Fatal编程技术网

C# mvc4 web api-将uint/int/enum视为long,将float/double视为double

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; } 此处的标签

我的任务是使用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;
 }
此处的标签如下所示:

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;由于跨境问题导致异常(无效转换)的语句

  • 如何解决这个问题
  • 为什么enum治疗得那么久?甚至我观察到uint、int、long、enum在调用堆栈中被视为long和float,double被视为double
  • 我可以得到如下的精确值

    对象值=空; tag.TryGetValue(tag.Property.DataType,out值); tmpTag.DataType=Convert.ToInt32(值)


  • 但是,与其逐个访问标记元素并将其转换为精确类型,还有什么最简单的方法可以自动转换并发送到另一个程序集吗?

    不转换它,您可以只执行简单的转换吗?同样,从int继承应该默认为INT32,但由于某些原因,您得到的是int64(long)。尝试显式地从int32继承