4

I'm trying to override a JsonConverter<T> in the System.Text.Json package which has an abstract Read method which can be ovveridden as:

public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)

(Link to custom serialization here.)

Here ref Utf8JsonReader marks reader as a ref struct. "ref struct" is a feature introduced in 7.2. (which if you're not familiar is a struct that's meant to be used only on the stack. MSDN Link)

In my organization, one of our projects runs on an old compiler that can target only upto C# 6. Is there a way to write MSIL or code in C# 6 that can override the method signature and implement a derivative class of the above signature?

Otherwise you will see the following error: Struct 'System.Text.Json.Utf8JsonReader' is obsolete: Types with embedded references are not supported in this version

17
  • 1
    The ref here designates the argument to be passed by reference instead of by value. It does not change the nature of the argument type. Also in your post please indicate which type is a structure and which is a class. Commented Jan 13, 2021 at 13:20
  • 1
    Can you please show what do you have now and what is the problem? You can't override what is overriden already. Pls show original code. Commented Jan 13, 2021 at 15:28
  • 2
    I know you can't make an override without the ref but do you need to? A ref struct is a new type of struct in C#, but that's not what you have here, here you have a ref parameter, which has been supported since way back. The question I'm asking is thus if you actually have a problem or if you have constructed a problem? You can use ref StructType parameters in C# 6. Commented Jan 14, 2021 at 11:25
  • 2
    That has nothing to do with the ref keyword in the parameter list, the struct then has to be a ref struct as well. Again, the ref keyword in the parameter list does not say that this is a ref struct, it says it is a ref parameter. Commented Jan 14, 2021 at 11:32
  • 1
    But let me answer your question. No, you won't be able to use this in C# 6. You will need to go (back) to Json.net, or update the compiler. Commented Jan 14, 2021 at 12:16

1 Answer 1

1

No, you cannot correctly implement a ref-struct based API using older versions of C# - the [Obsolete] is used very intentionally to prevent you from doing so (although in some other similar cases, a "mod-req" has been used to similar effect). ref struct has very specific demands, that could cause serious runtime problems if not satisfied (for example, an earlier compiler might attempt to box such a struct, which would be very very bad).

In short: update your C# version, or use an older version of the library which lacks this API which you cannot implement. As a third option, you could create your own library in a more recent version of C# that exists only to create an abstract implementation that overrides this API (perhaps with just throw new NotSupportedException();), and then inherit that from your down-level C# application. However, if the Utf8JsonReader API is now the primary (which I don't know, but could be the case), then this might cause the entire thing to be useless.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for confirming. I had already ended up doing a variant of option 3, i.e moving this API to another project so I don't need to compile this code with the old compiler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.