C# - How To Use A ref struct As A Generic Type Argument?

Viewed 389

I am learning C# and trying to implement an interface that uses generics. The interface is used within System.Text.Json custom converters.

Within the implementing class I am trying to pass a Utf8JsonReader as the type argument, (see code below).

The compiler gives an error message, that Utf8JsonReader cannot be used as a type argument. I think this is because it is a ref struct, which cannot be used as a type argument.

Interface


namespace WebApp.Data.Serializers.Converters.Visitors
{
    internal interface IVisitor<ReaderType>
    {
        MotionDetection DeserializeMotionDetection(ReaderType reader);
        MotionInfo DeserializeMotionInfo(ReaderType reader);
        MotionLocation DeserializeMotionLocation(ReaderType reader);
    }
}

Class

namespace WebApp.Data.Serializers.Converters.Visitors
{
    internal class JsonVisitor : IVisitor<Utf8JsonReader>
    {
        public MotionDetection DeserializeMotionDetection(Utf8JsonReader reader)
        {
            // implementation code
        }
        public MotionInfo DeserializeMotionInfo(ref Utf8JsonReader reader)
        {
            // implementation code
        }

        public MotionLocation DeserializeMotionLocation(ref Utf8JsonReader args)
        {
            // implementation code
        }
}

How can I work around this to implement the interface?

0 Answers
Related