c# - Can you group generic objects into a list? -
i have made machine converts biscuits 1 type another.
each biscuittransformer has method digestivebiscuittransformer:
ibiscuit transform(digestivebiscuit digestivebiscuit)
method.
is possible group these using common interface, given type different in each transformer, e.g. chocolatebiscuittransformer
ibiscuit transform(chocolatebiscuit chocolatebiscuit)
the gingernutbiscuittransformer:
ibiscuit transform(gingernutbiscuit gingernutbiscuit)
ideally, trying achieve, biscuittransformationmanager take in type of biscuit, , give ibiscuit back.
it load of transformers list<ibiscuittransformer>
and ibiscuittransformer expose type inputbiscuittype {get;}
compare incoming biscuit type:
biscuittransformationmanager:
ibiscuit transform<t>(t biscuit) { var transformer = loadtransformers().single(c => c.inputbiscuittype == typeof(t)); return transformer.transform(biscuit); }
i don't think works because transformer expecting concrete type, not t.
the problem can't figure out how group them, , if make interface method transform<t>
not sure how implement it, because t digestivebiscuit
in digestivebiscuittransformer
code accept anything, makes no sense.
any ideas?
i don't think need generics here.
how this:
ibiscuit transform(ibiscuit biscuit) { var transformer = loadtransformers().single(c => c.inputbiscuittype == biscuit.gettype()); return transformer.transform(biscuit); }
but, of course, means each of transformers should have matching method signatures instead of being type specific, think correct way go.
so instead of
ibiscuit transform(digestivebiscuit digestivebiscuit)
change transformer's signature (and others well) to:
ibiscuit transform(ibiscuit digestivebiscuit)
i think design cleaner , simpler trying make work generics.
Comments
Post a Comment