简体   繁体   English

反序列化枚举 C#

[英]Deserializing Enums C#

Is is possible to dynamically deserialize my enum string into enums.可以将我的枚举字符串动态反序列化为枚举。

In my API and my application that consumes my API i have this class.在我的 API 和使用我的 API 的应用程序中,我有这个 class。

 public static class ErrorCodes
    {
        public enum General
        {
            INCORRECT_PASSWORD,
            INCORRECT_USERNAME,
            INVALID_USERNAME
        }

        public enum ERRORTYPE_1
        {
            DATESPAN_NOT_PERMITTED
        }

        public enum ERRORTYPE_2
        {
            PERIOD_NOT_ALLOWED
        }
    }

In my API i have this.在我的 API 我有这个。 This is what is send back to my application in JSON form.这是以 JSON 形式发送回我的应用程序的内容。

public class Error
{

    public string Message { get; set; }
    public Enum Code { get; set; }

    public Error() { }

    public Error(Enum code)
    {
        this.Code = code;
    }

    public Error(string message, Enum code)
    {
        this.Message = message;
        this.Code = code;
    }
}

I can then return my error like this.然后我可以像这样返回我的错误。

new Error(ErrorCodes.ERRORTYPE_2.PERIOD_NOT_ALLOWED)

Is there a way to keep my enums seperate?有没有办法让我的枚举分开? I feel like a super long list of enum error codes is not very clean or maintainable.我觉得超级长的枚举错误代码列表不是很干净或可维护。

This blog post is pretty much what i want to do but with more abstraction of enums.这篇博客文章几乎是我想做的,但对枚举进行了更多抽象。 https://bytefish.de/blog/enums_json_net/ https://bytefish.de/blog/enums_json_net/

Is is possible to dynamically deserialize my enum string into enums?是否可以将我的枚举字符串动态反序列化为枚举?

If you are building a Web API project on dotnet-core, the solution in the blog you linked to works great, although I would use the standard System.Text.Json library (since core 3.0):如果您要在 dotnet-core 上构建 Web API 项目,那么您链接到的博客中的解决方案效果很好,尽管我会使用标准System.Text.Json库(si0885EE6333:

using System.Text.Json.Serialization;

namespace Foobar
{
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public enum Foo
    {
        Bar = 0,
        Baz = 1
    }
}

This will convert the enum Foo.Bar into "Bar" on the client, and "Bar" -> Foo.Bar on the server.这会将枚举Foo.Bar转换为客户端上的"Bar" Bar”,以及服务器上的"Bar" -> Foo.Bar

Is there a way to keep my enums separate?有没有办法让我的枚举分开? I feel like a super long list of enum error codes is not very clean or maintainable.我觉得超级长的枚举错误代码列表不是很干净或可维护。

I don't know if I can answer this part yet, because it looks like you've already done that with your class structure.我不知道我是否可以回答这部分,因为看起来你已经用你的 class 结构做到了。 I am confused about whether the code you gave is your goal or your current code.我对您提供的代码是您的目标还是您当前的代码感到困惑。


For more on serialization and deserialization of enums, check out this post .有关枚举的序列化和反序列化的更多信息,请查看这篇文章

When your Enum values are deserialized by the JSON Converter and sent back to the application, the enum values are converted to the Int32 type.当您的Enum值由 JSON 转换器反序列化并发送回应用程序时,枚举值将转换为Int32类型。 To be able to convert an int or string value to the Enum type, you should know the type of enum you want to convert/deserialize the value to.为了能够将intstring值转换为Enum类型,您应该知道要将值转换/反序列化为的枚举类型。 So you either need to declare all the ErrorCodes as part of a single Enum type (that way you know the target enum type), or include the typeof enum value in the response, so that can do Enum.Parse(typeof(TargetEnumType),enumValue) .因此,您要么需要将所有 ErrorCodes 声明为单个 Enum 类型的一部分(这样你就知道目标枚举类型),要么在响应中包含 typeof 枚举值,这样就可以做到Enum.Parse(typeof(TargetEnumType),enumValue) If you are using C# 8.0, the latter approach can be achieved by updating your Error class as below如果您使用的是 C# 8.0,则可以通过如下更新Error class 来实现后一种方法

public class Error<TEnum> where TEnum:Enum
{

    public string Message { get; set; }
    public TEnum Code { get; set; }


    public Type GetEnumType()
    {
        return Code?.GetType();
    }

    public Error() { }

    public Error(TEnum code)
    {
        this.Code = code;
    }

    public Error(string message, TEnum code)
    {
        this.Message = message;
        this.Code = code;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM