MMPClassEnumerator 1.0.0.23
See the version list below for details.
dotnet add package MMPClassEnumerator --version 1.0.0.23
NuGet\Install-Package MMPClassEnumerator -Version 1.0.0.23
<PackageReference Include="MMPClassEnumerator" Version="1.0.0.23" />
paket add MMPClassEnumerator --version 1.0.0.23
#r "nuget: MMPClassEnumerator, 1.0.0.23"
// Install MMPClassEnumerator as a Cake Addin #addin nuget:?package=MMPClassEnumerator&version=1.0.0.23 // Install MMPClassEnumerator as a Cake Tool #tool nuget:?package=MMPClassEnumerator&version=1.0.0.23
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
- Inspiration code: https://stackoverflow.com/a/699871
- Template for Readme file is from https://gist.github.com/PurpleBooth
- Icon from https://iconarchive.com/show/ios7-icons-by-icons8/Programming-Cls-icon.html
Source code
The source code for this project is available at https://github.com/MarcusMedinaPro/MMPClassEnumerator
Product | Versions 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. |
-
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.