samedi 27 novembre 2021

C# extendable class library - how to reference extension object

What is the best way to reference objects coming from possible extension methods?

I have a class library FooApi.Core which exposed the public class FooApi.Core.Client. This can be used on its own, but what if I want to give the user the possibilty to extend this library with e.g FooApi.WebApi?

To add the extension to FooApi.Core.Client I added a extension method in FooApi.WebApi which should add the web api object to the Client.

namespace FooApi.WebApi
{
    public static class Extensions
    {
        public static FooApi.Core.Client AddWebApi(this FooApi.Core.Client client)
        {
            // that already requires a WebApi object in core client / reference to the project i want to be extendable
            client.WebApi = new WebApi();
            return client;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire