RSCG – Enhanced.GetTypes

RSCG – Enhanced.GetTypes
 
 

name Enhanced.GetTypes
nuget https://www.nuget.org/packages/Enhanced.GetTypes/
link https://github.com/duskembayev/Enhanced.GetTypes
author duskembayev

Generating list of PUBLIC classes that implements an interface

 

This is how you can use Enhanced.GetTypes .

The code that you start with is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
<Project Sdk="Microsoft.NET.Sdk">
 
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
 
  <ItemGroup>
    <PackageReference Include="Enhanced.GetTypes" Version="1.0.0" />
  </ItemGroup>
    <PropertyGroup>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
    </PropertyGroup>
</Project>

The code that you will use is

1
2
3
4
5
6
7
8
// See https://aka.ms/new-console-template for more information
using GetTypesForInterface;
 
Console.WriteLine("Hello, World!");
foreach (var type in ProjectTypes.GetIPersonTypes())
{
    Console.WriteLine(type.Name);
}
1
2
3
4
5
6
7
8
using Enhanced.GetTypes.Annotation;
 
namespace GetTypesForInterface;
public partial class ProjectTypes
{
    [DerivedTypes(typeof(IPerson))]
    public  static partial IEnumerable<Type> GetIPersonTypes();
}
1
2
3
4
5
namespace GetTypesForInterface;
internal interface IPerson
{
    public string Name { get; set; }
}
1
2
3
4
5
6
namespace GetTypesForInterface;
public class Student : IPerson
{
    public string Name { get; set; } = "";
}
    
1
2
3
4
5
namespace GetTypesForInterface;
public class Teacher:IPerson
{
    public string Name { get; set; } = "";
}

 

The code that is generated is

01
02
03
04
05
06
07
08
09
10
11
12
13
// <auto-generated />
namespace GetTypesForInterface
{
    partial class ProjectTypes
    {
        public static partial System.Collections.Generic.IEnumerable<System.Type> GetIPersonTypes()
        {
            yield return typeof(GetTypesForInterface.Student);
            yield return typeof(GetTypesForInterface.Teacher);
            yield break;
        }
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Enhanced.GetTypes