确保存在必要的枚举值并用 EnumMemberAttribute 属性标记
Ensure that the necessary enum values are present and are marked with EnumMemberAttribute attribute
当我通过 WCF 将此 DTO 发送到我的服务器端时,出现此错误:
There was an error while trying to serialize parameter http://tempuri.org/:objects. The InnerException message was 'Enum
value '3' is invalid for type 'LPFLogger' and cannot be serialized.
Ensure that the necessary enum values are present and are marked with
EnumMemberAttribute attribute if the type has DataContractAttribute
attribute.'. Please see InnerException for more details.
我的 DTO 没有 DataContract 属性,我根本不使用它们。默认情况下不需要它。
为什么值 3 是个问题?因为我的枚举只有 0,1,2 ?
public class MyObjectDTO
{
public int Id { get; set; }
public int Size { get; set; }
public LPFProcessor Processor { get; set; }
public LPFAccess Access { get; set; }
public LPFLogger Logger { get; set; }
}
public enum LPFLogger
{
None = 0,
EventLogger,
TempLogger
}
请找到下面的代码来检查枚举是否已经存在于您的服务器端
private static LPFLogger GetLPFLogger(int value)
{
var lstEnum = Enum.GetValues(typeof(LPFLogger)).Cast<int>().ToList();
if (lstEnum.Any(s => s == value))
return (LPFLogger)value;
return LPFLogger.None;
}
当我通过 WCF 将此 DTO 发送到我的服务器端时,出现此错误:
There was an error while trying to serialize parameter http://tempuri.org/:objects. The InnerException message was 'Enum value '3' is invalid for type 'LPFLogger' and cannot be serialized. Ensure that the necessary enum values are present and are marked with EnumMemberAttribute attribute if the type has DataContractAttribute attribute.'. Please see InnerException for more details.
我的 DTO 没有 DataContract 属性,我根本不使用它们。默认情况下不需要它。
为什么值 3 是个问题?因为我的枚举只有 0,1,2 ?
public class MyObjectDTO
{
public int Id { get; set; }
public int Size { get; set; }
public LPFProcessor Processor { get; set; }
public LPFAccess Access { get; set; }
public LPFLogger Logger { get; set; }
}
public enum LPFLogger
{
None = 0,
EventLogger,
TempLogger
}
请找到下面的代码来检查枚举是否已经存在于您的服务器端
private static LPFLogger GetLPFLogger(int value)
{
var lstEnum = Enum.GetValues(typeof(LPFLogger)).Cast<int>().ToList();
if (lstEnum.Any(s => s == value))
return (LPFLogger)value;
return LPFLogger.None;
}