Replies: 0 comments 9 replies
-
NET 7.0 |
Beta Was this translation helpful? Give feedback.
-
Every record overrides |
Beta Was this translation helpful? Give feedback.
-
Tagging subscribers to this area: @dotnet/area-meta Issue DetailsIs it a bug that I find that override ToString is invalid from the abstract record base type and can only be effective from Derived Class? This will override many Derived ToString methods if it cannot be overwritten from the abstract record, which is cumbersome for many Derived classes. I have one more suggestion for record: Add an Attribute that ignores PrintMember, so instead of each override Derived Class override PrintMembers method, Simply add the specified Attribute on a particular attribute or field to ignore (similar to '[JsonIgnore]') For the PrintMemberAttribute suggestion, the attributes can be: IgnorePrintMembers, IgnoreNull If the compiler can do this automatically at compile time, you can subtract a lot of unnecessary work and time (which shouldn't be hard to do, because that's what the compiler or code generator does). Test the modelusing System.Text;
var models = new List<BaseModel>() { new ModelA(), new ModelB(), };
foreach (var model in models)
{
Console.WriteLine(model.ToString());
Console.WriteLine(model.ToString2());
Console.WriteLine();
}
enum ModelType { Unknown, ModelA, ModelB }
abstract record BaseModel
{
public virtual string RawType { get; init; } = "Unknown";
public ModelType Type => Enum.Parse<ModelType>(RawType);
protected virtual bool PrintMembers(StringBuilder builder)
{
builder.Append($"RawType = {RawType}");
return true;
}
public override string ToString() => "Override ToString!";
public virtual string ToString2() => "ToString2!";
}
record ModelA : BaseModel
{
public override string RawType { get; init; } = "ModelA";
public string ExtraA { get; init; } = "ExtraA";
}
record ModelB : BaseModel
{
public override string RawType { get; init; } = "ModelB";
public string ExtraB { get; init; } = "ExtraB";
public override string ToString() => "Override ModelB ToString!";
public override string ToString2() => "Override ModelB ToString2!";
} Running results
|
Beta Was this translation helpful? Give feedback.
-
I think we should check if |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Moving this to Roslyn since this issue is about what the compiler emits for derived record types. It sounds to me like @Bunnui is requesting some way to tell the compiler not to implement ToString on the derived type. |
Beta Was this translation helpful? Give feedback.
-
Moving to csharplang. Roslyn is emitting correctly as per the specification afaict. This is a request that the specification around records either be changed, or have some update to it to allow customization of this behavior. Our currently position though is that we are highly unlikely to make any changes here. Instead, you can fully customize the behavior of records by supplying custom implementations yourself. And, for something like:
This is already possible today if someone wants to write a source-generator. Just define those attributes, and write a source-generator that spits out the methods you want that behave with the semantics you want those attributes to have. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Is it a bug that I find that override ToString is invalid from the abstract record base type and can only be effective from Derived Class? This will override many Derived ToString methods if it cannot be overwritten from the abstract record, which is cumbersome for many Derived classes.
I have one more suggestion for record: Add an Attribute that ignores PrintMember, so instead of each override Derived Class override PrintMembers method, Simply add the specified Attribute on a particular attribute or field to ignore (similar to '[JsonIgnore]')
For the PrintMemberAttribute suggestion, the attributes can be: IgnorePrintMembers, IgnoreNull
If the compiler can do this automatically at compile time, you can subtract a lot of unnecessary work and time (which shouldn't be hard to do, because that's what the compiler or code generator does).
Test the model
Running results
Beta Was this translation helpful? Give feedback.
All reactions