Question

Can I setup a Dictionary of MyDict<Type, BlockingCollection<"differentTypes"> possibly through an interface that circumvents any casting thus preventing the usage of object or dynamic types?

The reason I want to use such is because I want to initialize Blocking Collections of different types at runtime because incoming objects are of different types and I then want to add such incoming objects to the type matching Blocking Collection. I also want to access the objects within the BlockingCollection later on without having to cast. The necessary types of the Blocking collection are known at the instantiating time of the wrapping class. The types of the incoming object are known at runtime only. Incoming object types will never be different from the types known at the class instantiation time. The types of the objects that I want to retrieve from the matching BlockingCollection are also only known at runtime. Any way to set this up without having to cast?

I browsed through many related questions but did not find the exact same specs described here.

Was it helpful?

Solution

Ultimately, "differentTypes" means either object, or a common base-class / interface. If those types are value-types, then they will have to be boxed and unboxed (although you indicate that these are class objects so this doesn't apply - only structs are ever boxed/unboxed). So that leaves a few casts - casts are pretty stonkingly fast: I wouldn't be too concerned about the impact of that.

Of course, if there is some commonality between the "differentTypes" (some common interface etc), then you might be able to use that common interface without ever casting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top