I want have a "pointer" to an object but the object can be one of two classes.
QuickFix.Message newOrderSingle;
if (ecn.versionFIX.Equals(VersionFIX.FSS_FIX44))
{
newOrderSingle = new QuickFix.FIX44.NewOrderSingle(
new ClOrdID(masterForm.OrderBook.GetNewClOrdIDBroker(ecn.brokerCode)),
new Symbol(symbol),
new Side(side),
new TransactTime(DateTime.Now),
ordType = new OrdType(OrdType.LIMIT));
}
else
{
newOrderSingle = new QuickFix.FIX42.NewOrderSingle(
new ClOrdID(masterForm.OrderBook.GetNewClOrdIDBroker(ecn.brokerCode)),
new HandlInst('1'),
new Symbol(symbol),
new Side(side),
new TransactTime(DateTime.Now),
ordType = new OrdType(OrdType.LIMIT));
}
Then later I want to do this, where "set" is a method of QuickFix.FIX44.NewOrderSingle
:
newOrderSingle.Set(new Price(limitPrice));
Instead I have to do:
((QuickFix.FIX44.NewOrderSingle) newOrderSingle).Set(new Price(limitPrice));
Which is hard to read.
Can I change "cast" of NewOrderSingle
dynamically in some way?
As long as you use the common base class
QuickFix.Message
you cannot use specific members without casting.If you have a piece of code where you work with a specific subclass you can do:
You have some options:
dynamic
You can use
dynamic
keyword to make "duck typing":Unfortunately, you loose intellisense and will get
RuntimeBinderException
whenorder
has not such a method (is of typeFIX42
f.e.).GenericInvoker
You can use my library:
Unfortunately, you need to hardcode type.
To sum up If you need to use such approach, your classes are badly designed. Consider creating base / abstract class or some inteface:
then:
if you have access to the source code of
QuickFix.Message
, you can add it. perhaps you can add the set function to a common interface.the really dirty way is using reflection. the code would look like this:
(I guess it will not compile directly, the function parameters needs to be adjusted)
you could also try to use the
dynamic
datatype