简体   繁体   中英

C# Exporting interface as dll in an existing project with custom variable types

I'm creating a project in which I made my own classes but now I need to support plugins. So I need an interface to then export as dll, so people can create plugins using that dll as the interface. Now I'd like the plugin that is being created to be able to use my custom types.

For example:

My project has a class named Product. I then created an interface with the following method:

Product[] getProducts();

Then a user that creates a plugin overrides that method and needs to have access to the Product class. How am I going to do this?

I've tried to compile my whole project as a .dll but that just gives alot of errors. I've also tried to compile the neccesary project files to a .dll but my project refers to almost every part so it still has errors.

I'd like to google it, but I've no idea what to google on.

Thanks.

Edit: I'm using Visual C# 2013 express edition.

Create a separate project that only contains interfaces. Your main product would reference this assembly and the extensions would also reference this assembly.

For example, lets say you want extensions to provide a method GetAnimals() which returns an IEnumerable. Your second project would contain an interface that looks like this:

public interface IAnimal
{
    UInt32 Height { get; }
}

public interface IExtension
{
    IEnumerable<IAnimal> GetAnimals();
}

The project would be of type class library and would only contain these interfaces. The project would not reference your main project. Your main project would reference this class library and you could give the class library to your users/customers/extenders and they could build extensions by implementing IExtension.

Update: A key in making extensions is that your users will never have direct access to any of your concrete types. They will always only have access to the interfaces that your concrete types implement. You will need to structure your application to have interface hierarchies for everything so that you can provide those interfaces to the user. You want to avoid putting any concrete types into your extension class library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM