MMPClassEnumerator 1.0.0.24

dotnet add package MMPClassEnumerator --version 1.0.0.24                
NuGet\Install-Package MMPClassEnumerator -Version 1.0.0.24                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MMPClassEnumerator" Version="1.0.0.24" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MMPClassEnumerator --version 1.0.0.24                
#r "nuget: MMPClassEnumerator, 1.0.0.24"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MMPClassEnumerator as a Cake Addin
#addin nuget:?package=MMPClassEnumerator&version=1.0.0.24

// Install MMPClassEnumerator as a Cake Tool
#tool nuget:?package=MMPClassEnumerator&version=1.0.0.24                

ClassEnumerator

This project is a small class with the only purpose to provide the coder with a list of classes implemmenting a specific interface. This is done by asking for classes that match a specific Interface or classes that inherit from a specific class. The whole idea is to make it easier to use plugins or solutions of that kind.

  • The main reason I made it a nuget was to show my students how to create and publis nugets. *

Getting Started

You can either use the Nuget manager in Visual Studio, or install it from the package-manager. For more information see docs.microsoft.com.

Prerequisites

If you need a list of classes without having to code a control to create an instance of each of them.

for(int i=0; i<MyStuff.Count; i++)
    Console.WriteLine($"{i:00} {MyStuff[i].Name}");

Console.Write("Select a topic: ");
var input = Console.ReadLine();
_ = int.TryParse(input, out var choice);

ITopic topic;
switch(choice())
{
    case 0: topic=new Topic0(); break;
    // A long list of cases follows....
}

Installing

You can use the Nuget manager in visual studio or simply type

install-package MMPClassEnumerator

Using the nuget

To get a list of instansiated classes that match your interface, you type

var listOfStuff = EnumerateClasses<IStuff>.GetClassesByInterface();

If you only want the name of the classes that match your interface, you type

var listOfStuff = EnumerateClasses<IStuff>.ListClassesByInterface();

To list the classes you could use

ListOfStuff.ForEach(i => Console.WriteLine(i.Name));

The nuget provides you with four methods

// Gets instances of classes that match the given parent class
GetClassesByInheritance<T>()

// Gets instances of classes that match the given Interface
GetClassesByInterface<T>()

// Gets definitons of classes that match the given parent class
ListClassesByInheritance<T>()

// Gets definitons of classes that match the given Interface
ListClassesByInterface<T>()

Usage example

var listOfStuff = EnumerateClasses<IStuff>.GetClassesByInterface();
for(int i=0; i<MyStuff.Count; i++)
    Console.WriteLine($"{i:00} {MyStuff[i].Name}");```

Console.Write("Select a topic: ");
var input = Console.ReadLine();
_ = int.TryParse(input, out var choice);

ITopic topic=MyStuff[choice];

Other example, to list all classes in a project

var lst = EnumerateClasses<object>.GetClassesByInheritance().ToList();
lst.ForEach(i => Console.WriteLine(" class " + i.GetType().Name + "()"));

Deployment

The deployment to a nuget is triggered by scripts on GitHub after PR to the main repository.

Built With

  • C#, .net 6

Authors

  • Marcus Medina - Initial project

License

Since I found the reflection-Linq-code on StackOverflow I am publishing this project according to the license they use.

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Acknowledgments

Source code

The source code for this project is available at https://github.com/MarcusMedinaPro/MMPClassEnumerator

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0.24 510 1/11/2022
1.0.0.23 271 1/5/2022