0

I've seen this asked, but the standard answer is

An interface is a way to define a contract to interact with an object.

This is all and well, but I'm in need of a way for a class to describe itself to allow its creation. Specifically, I have interface ITicket which defines an object responsible for selling/buying assets. Different implementations require different parameters. My reflex would have been to do something that looks like:

public interface ITicket{
    static List<TicketOptions> GetAvailableOptions();
}

public class TicketOption{
    public string Label { get; set; }
    public string Type { get; set; }
    public string Default { get; set; }     
}

Then I could have selected an implementation of ITicket in my GUI, and looped over the parameters to create an interface with IntegerUpDown controls for integers, DecimalUpDown controls for decimals and dropdown boxes for Enums.

Alas, C# won't let me. So here I am, looking for an equivalent. Surely there must be a pattern to let me define a contract to interact with a class without an instance?

Edit: Getting into more details...

My C# application loads IronPython scripts. It scans the /Scripts folder and assumes every python file in there contains a class called Ticket implementing ITicket.

I would like to get a list of available parameters for every script to build an interface. This way developpers can create python scripts that they drop into a folder that add new complex behavior without re-compiling the application.

Everything works well, except automatically (and cleanly) knowing what parameters are needed.

Benoit Dufresne
  • 318
  • 3
  • 9
  • 1
    why use `static` in interface declaration? remove it and proceed – ASh Mar 29 '17 at 13:39
  • 1
    I dont really understand what you are trying to do. As far as I understand you, you have different implementations of ITicket that needs different parameters for constructions. Look into the Factory patterns, that should help you. – Andre Mar 29 '17 at 13:51
  • @ASh I don't have an instance to work with until I create it. I need to know what parameters it wants before creating it. – Benoit Dufresne Mar 29 '17 at 14:01
  • @Andre How does my factory know about the parameters the implementations need? As far as I can tell any time I change a parameter in an ITicket implementation, I'll need to modify the Factory accordingly? This is what I'm trying to avoid. – Benoit Dufresne Mar 29 '17 at 14:04
  • You won't be able to resolve this. Either you retrieve your options by reflection (which most probably is not the preferred option) or you need to have an instance (or static method in a class, not an interface) that knows about the options of your ticket types. If you need such flexibla tickets also consider a generic Ticket implementation that is just a container for TicketOptions, but this depends on your need for flexibility / statically typed objects – cmonsqpr Mar 29 '17 at 14:27
  • @Benoit Dufresne You could use a DI container for that. The DI container can work as a factory and you have no need to do additional work in case your implementations constructor parameters change. – Andre Mar 30 '17 at 05:51

0 Answers0