Skip to content

Repository files navigation

AutoInterface

C# Source Generator which redirects all interface-calls to one or more backing members. Source code can be generated for a class, record, or struct. It can be generated automatically or by a custom template (scriban, liquid).

Manually written source:

interfaceIPrintable{voidPrint();}publicpartialclassPerson:IPrintable{[BeaKona.AutoInterface]privatereadonlyIPrintableaspect1=newPersonPrinterV1();}

Auto-generated accompanying source:

partialclassPerson{voidIPrintable.Print()=>this.aspect1.Print();}

Ad-hoc adapter pattern

In this example PersonPrinterV1 does not implement IPrintable but does have all members that are required by that interface.

Manually written source:

interfaceIPrintable{voidPrint();}classPersonPrinterV1{voidPrint(){ ...}}publicpartialclassPerson{[BeaKona.AutoInterface(typeof(IPrintable))]privatereadonlyPersonPrinterV1aspect1=newPersonPrinterV1();}

Auto-generated accompanying source:

partialclassPerson:IPrintable{voidIPrintable.Print()=>this.aspect1.Print();}

Generate code from a template

Manually written source:

interfaceIPrintable{voidPrint();}publicpartialclassPerson:IPrintable{// add file mytemplate.scriban in your VS project// and set it's build action to: 'C# analyzer additional file'[BeaKona.AutoInterface(TemplateFileName="mytemplate.scriban")]privatereadonlyIPrintable?aspect1=null;}

Auto-generated accompanying source:

partialclassPerson{
..generatedfrom file..
}

Partial template

Partial template can be defined inline (from string) or from file.

Manually written source:

interfaceIPrintable{intLength{get;}intCount{get;}voidPrint1();voidPrint2();}publicpartialclassPerson:IPrintable{privatevoidLogDebug(stringname){}[BeaKona.AutoInterface][BeaKona.AutoInterfaceTemplate(BeaKona.AutoInterfaceTargets.PropertyGetter,Filter="Length",Language="scriban",Body="return 1;")][BeaKona.AutoInterfaceTemplate(BeaKona.AutoInterfaceTargets.Method,Filter="Print(\\d)?",Body="LogDebug(nameof({{interface}}.{{name}})); {{expression}};")]privatereadonlyIPrintable?aspect1=newPrinterV1();}

Auto-generated accompanying source:

partialclassPerson{intIPrintable.Length{return1;}intIPrintable.Count=>this.aspect1!.Count;voidIPrintable.Print1(){LogDebug(nameof(IPrintable.Print1));this.aspect1!.Print1();}voidIPrintable.Print2(){LogDebug(nameof(IPrintable.Print2));this.aspect1!.Print2();}}

Other examples can be found in wiki.


---

.NET CoreNuGet

About

C# interface-to-member source generator

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages