RadovanCommonExtensions - The C# package I use in ALL my projects! 📚

Article by pkg-dot-zip
Published Wed, 12 Mar 2025 13:30:00 GMT (last edited Wed, 12 Mar 2025 17:59:00 GMT)

Ever had the need to reuse code from another project you made? Same!

When I was working on some assignments from university (and some big personal ones 👀) I noticed I kept writing the same extension methods over and over again. Looks like we need our own library! 😎

Question

Are you using this and missing something? Please let me know if you have suggestions! 😁


The Premise

I noticed that I kept writing the same extension methods in each project. For example, back in my Java days, I had the beautiful .isEmpty() method on collections. Not only that, but I could actually use .AddAll() on anything! In C# .AddRange() is only available on Lists!

So C# took that all away from me! 😡 💢

Well, that is kind of a silly reason to make a library, I obviously have more advanced use cases. Like some specific reflection techniques!

 public static object? InvokeTypeWithGenerics(this Type type, string methodName, Type[] genericTypes,
            object instance, object[]? parameters = null)
{
    var methodToInvoke = type.GetMethod(methodName,
        BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public |
        BindingFlags.NonPublic);

    if (methodToInvoke is null)
        throw new ArgumentException($"Method with name {methodName} in type {type.Name} does not exist.");
    return methodToInvoke.MakeGenericMethod(genericTypes).Invoke(instance, parameters);
}

What is going on here? 🤔 Well, in one of my projects I NEED to call a method with all kind of types, but generics in C# need to be tied down (generic constraints) in some cases. This was my implementation to bypass these constraints. This is not good for performance, but that was not the goal of the project. Interesting still right? 🤔

So we have some missing methods, and some specifics I need in two or more projects. I won't let Microsoft beat me on this one; time to make our own library. 🤓


The Goal

The goal of this project was to create a library anyone can install via NuGet and use in all their C# projects. However, C# has all kind of targets, like core, 8.0, 9.0-android and more. If only there was a way to make a project compatible with all...

There is! It's called standard! So let's create a project using that! 😋


How Does This Work?

Very simple! Create a solution with a project targeting .net standard 2.1, then create another project for unit tests and target .net 8.0! It is that easy. 😎

So implementing a method is as simple as adding it:

public static void AddAll<T>(this ICollection<T> collection, IEnumerable<T> elements)
{
    foreach (var element in elements) collection.Add(element);
}

And then running the unit tests! 👨🏻‍🔬


Where can I get it, so I can use it myself?

I obviously had to upload it to NuGet so people (read other students) can use it for themselves.

The repository is available here.
The package is available here.