Skip to content

naratteu/Naratteu.StrongDuck

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Naratteu.StrongDuck

DuckTyping SourceGenerator for C#

How to use

This is a style inspired by [JsonSerializable(typeof(T))]

[Duck(typeof(A))]
[Duck(typeof(B))]
[Duck(typeof(..))]
interface IDuck
{
    string Quack();
    string Flap();
}

위 코드만 입력하면, AB에 선언된 public string Quack()public string Flap() 함수를 IDuck의 맴버와 직결하는 class를 생성합니다.

만일 대응하는 함수가 없다면 컴파일에러가 발생하며, 코드수정이 불가한 타입일경우 확장함수를 덧붙이는것으로 해결합니다.

IDuck[] ducks = [
    new A().ToDuck<IDuck>(),
    new B().ToDuck<IDuck>(),
    ..
];

이후, 위와같이 관계없는 타입을 동일타입으로 변환해 취급할수 있게됩니다. Playground 에서 테스트해보실 수 있습니다.

Todo

  • Support Property, Indexer, etc..

About

DuckTyping SourceGenerator for C#

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages