Замок Windsor, решающий и дженерики

Проще говоря, вы можете использовать .contains() для String «правильных» символов.

Что-то вроде:

    private final String letters = "abcdefghijklmnopqrstuvwxyz";

    @Override
    public void keyPressed(KeyEvent e) {
        char c = e.getKeyChar();            

        // if the character entered is not the list of valid inputs, warn user 
        if( !letters.contains(Character.toString(c)) ){
            JOptionPane.showMessageDialog(panel, "Error: "+c, " cannot be used", JOptionPane.WARNING_MESSAGE);
        }
    }
6
задан vdhant 16 May 2009 в 13:15
поделиться

1 ответ

vdhant - That's not how containers are meant to be used.

You want to use ISubject, right?. Then if you passed T you're breaking your abstraction, because your caller must know that ISubject, is actually a Subject, and more than that, its a Subject and that it requires a concrete T.

No container will allow that, but it's a design problem, not tool problem.

One thing to fix your design, would be to make it explicit - change ISubject to ISubject

Then you could register open generic type ISubject<> and bind it to open generic type Subject<>.

container.Register(Component.For(typeof(ISubject<>))
                            .ImplementedBy(typeof(Subject<>)));

Then you'd be able to do

var fooSubject = container.Resolve<ISubject<Foo>>();

You didn't provide any context so I may be off the track with the answer, but one thing is for sure - you have a design problem.

12
ответ дан 9 December 2019 в 20:48
поделиться
Другие вопросы по тегам:

Похожие вопросы: