-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a JsonConverter.Type property. #63898
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsDescription
Error CS0122 'JsonConverter.TypeToConvert' is inaccessible due to its protection level CustomJsonStringEnumConverter.cs Reproduction Steps
Expected behaviorThis property should be accessible to derived classes
Actual behaviorError CS0122 'JsonConverter.TypeToConvert' is inaccessible due to its protection level CustomJsonStringEnumConverter.cs Regression?No response Known WorkaroundsDefine my own copy of the method in the derived class. ConfigurationNo response Other informationNo response
|
Why do you need to access the method? |
when derivind a class, some logic is based on that information. Just need to read the prop. |
In derived class, you should have enough access to the type argument provided. Accessing |
Yes you are right, but duplicated logic can and should be avoided. |
I don't think the property is really "logic". It should be an architectural helper since the non-generic base class has no access with type argument. |
Hi @huoyaoyuan. Currently if a user wants to derive a class from My suggestion what to simply expose that property so that derived classes would have access to it. and avoid the developers from having to having to copy the code (which is what I had to do). If it's useful in the base class.... why could it not be useful in the derived class? Of course there is a workaround, but I just thought I would point it out. This library is slowly making it's way into mainstream, and maybe I use But again there is an simple workaround, in principle, duplicating code and/or logic should be avoided, that is all. |
Because it is
You'd better provide a sample to demonstrate how you would use the property if accessible. |
I think it's a useful addition, for example somebody might want to index untyped |
This issue has been marked |
Hi @huoyaoyuan,
That is exactly my point.... right now it's an
all I am asking is that it becomes
cc: @eiriktsarpalis |
The it's required to be |
Hi @huoyaoyuan, The property is set as When deriving custom classes from You seem to think otherwise, let's leave it at that. |
Yes I think it should be public. I don't think there is much benefit from marking it protected since you almost always inherit from the typed |
Upon further consideration, I don't think we should expose |
That is a strange conclusion; since as you say.. it’s just a typeof, furthermore, the developer eho whishes to create a derivative should be responsible enough,as with any other case, test and prevent the “possible” undefined behaviors. But I guess that is your call! |
@robert-stratecglobal what should be the value of |
I think we could contemplate a design where the property is nullable. This should cover the case of public abstract class JsonConverter
{
internal JsonConverter() { }
+ public abstract Type? Type { get; }
}
public abstract class JsonConverter<T> : JsonConverter
{
+ public sealed override Type Type => typeof(T);
}
public abstract class JsonConverterFactory : JsonConverter
{
+ public sealed override Type? Type => null;
} |
Looks good as proposed public abstract class JsonConverter
{
internal JsonConverter() { }
+ public abstract Type? Type { get; }
}
public abstract class JsonConverter<T> : JsonConverter
{
+ public sealed override Type Type => typeof(T);
}
public abstract class JsonConverterFactory : JsonConverter
{
+ public sealed override Type? Type => null;
} |
I missed the API review, but I had this question: public abstract class JsonConverterFactory : JsonConverter
{
+ public sealed override Type? Type => null;
} Because this is |
The property is meant as a proxy for the |
EDIT see #63898 (comment) for an API proposal
Original Proposal
Description
internal sealed override Type TypeToConvert => typeof(T);
Error CS0122 'JsonConverter.TypeToConvert' is inaccessible due to its protection level CustomJsonStringEnumConverter.cs
Reproduction Steps
Expected behavior
This property should be accessible to derived classes
protected internal sealed override Type TypeToConvert => typeof(T);
Actual behavior
Error CS0122 'JsonConverter.TypeToConvert' is inaccessible due to its protection level CustomJsonStringEnumConverter.cs
Regression?
No response
Known Workarounds
Define my own copy of the method in the derived class.
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: