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
<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
// 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); }
using Enhanced.GetTypes.Annotation; namespace GetTypesForInterface; public partial class ProjectTypes { [DerivedTypes(typeof(IPerson))] public static partial IEnumerable<Type> GetIPersonTypes(); }
namespace GetTypesForInterface; internal interface IPerson { public string Name { get; set; } }
namespace GetTypesForInterface; public class Student : IPerson { public string Name { get; set; } = ""; }
namespace GetTypesForInterface; public class Teacher:IPerson { public string Name { get; set; } = ""; }
The code that is generated is
// <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
Leave a Reply