Last active
July 6, 2021 05:24
-
-
Save wipiano/59849fd5c0182282c1cf00ca48814fb7 to your computer and use it in GitHub Desktop.
SourceGenerator で実装された PocoFreezer がほしい
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FreezableObjectAttribute : Attribute | |
{ | |
public bool GenerateRecord { get; set; } | |
} | |
public class PocoFreezerGenerated : Attribute{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SourceGenerator で生成されるコード | |
[PocoFreezerGenerated] | |
public partial record FrozenSamplePoco1(string Hoge, int Fuga); | |
[PocoFreezerGenerated] | |
public partial class FrozenSamplePoco2 | |
{ | |
public FrozenSamplePoco2(string hoge, int fuga) | |
{ | |
Hoge = hoge; | |
Fuga = fuga; | |
} | |
public string Hoge { get; } | |
public int Fuga { get; } | |
} | |
public static partial class SamplePoco1Freezer | |
{ | |
public static FrozenSamplePoco1 Freeze(this SamplePoco1 source) | |
{ | |
return new FrozenSamplePoco1(source.Hoge, source.Fuga); | |
} | |
} | |
public static partial class SamplePoco2Freezer | |
{ | |
public static FrozenSamplePoco2 Freeze(this SamplePoco2 source) | |
{ | |
return new FrozenSamplePoco2(source.Hoge, source.Fuga); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ユーザが書くコード | |
[FreezableObject(GenerateRecord = true)] | |
public class SamplePoco1 | |
{ | |
public string Hoge { get; set; } | |
public int Fuga { get; set; } | |
} | |
[FreezableObject(GenerateRecord = false)] | |
public class SamplePoco2 | |
{ | |
public string Hoge { get; set; } | |
public int Fuga { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment