Newex / PartialSourceGen

Generate new class/struct/record with all properties as nullable by source generation using the Partial attribute.
MIT License
6 stars 2 forks source link

Handle inheritance from a partial entity #16

Open Newex opened 6 days ago

Newex commented 6 days ago

What? Given an abstract class and a derived type have a partial class that inherits from the partial abstract class.

Example:

[Partial]
public abstract class Person
{
   public string Name { get; set; }
}

// Syntax suggestions welcome...
[Partial(DerivedFromAbstract = typeof(PartialPerson))]
public class Employee : Person
{
}

// Output partial person
public partial abstract class PartialPerson
{
   public string? Name { get; set; }
}

// Output partial employee
public partial class PartialEmployee : PartialPerson
{
}

Why?
So you can have a more generic method that accepts the abstract partial class.

Example:

public void UpdateInfo(PartialPerson person)
{
   // todo
}
Newex commented 6 days ago

Or maybe have the syntax similar to JsonDerivedType attribute in System.Text.Json.Serialization namespace.

[PartialDerivedType(typeof(PartialPerson))]
public class Employee : Person
{
}

Although I cannot imagine how this would be implemented due to the extra arguments existing in Partial attribute.