我的任务是使用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;
}
}
}
这里当前的问题是,当我发送下面的JSON请求时,枚举NativeDataType正在处理: { “名称”: “ABCD”, “数据类型”:2}
不幸的是,Tag类是在另一个程序集中定义的。因为那个
tmpTag.DataType = tag.DataType;由于跨境问题导致异常(无效演员)的声明。
我可以得到如下的确切值
object value = null; tag.TryGetValue(Tag.Property.DataType,out value); tmpTag.DataType = Convert.ToInt32(value);
但是,而是逐个访问Tag元素并将它们转换为确切类型,是否有最简单的方法可以自动转换并发送到另一个程序集?
答案 0 :(得分:0)
你可以做一个简单的演员而不是转换吗?同样继承自int应该默认为INT32,但出于某种原因你得到int64(long)。尝试显式继承int32。