The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral
You can't downcast from a base class to a derived class.
That should be your problem.
The rule only applies when you go from a base class up to a derived class.
Why downcasting from Object back to the original type works is becoz it uses unboxing, instead of casting. which retains the object's original structure type. And don't try casting from ManagementBaseObject -> Object -> ManagementClass because boxing will throw an exception coz of type mismatch.
Whereas when you downcast from a base class to a derived class, your base class does not have the additional information of the derived class to be created as a new derived type. That's the problem. That's where interfaces come in. Interfaces guarantees that you have those methods within your objects, else it'll throw an exception before you even start using it.
I don't know much about WMI provider, but the differences I see from ManagementClass and ManagementObject to ManagementBaseObject is very small. Unless you're meant to work in ManagementClass, I'm sure the ManagementBaseObject will provide you with everything you need.
If you can show some code, and which class/method you're using, maybe I can help a bit. Coz I've never used WMI before.
triplez wrote:Whereas when you downcast from a base class to a derived class, your base class does not have the additional information of the derived class to be created as a new derived type. That's the problem. That's where interfaces come in. Interfaces guarantees that you have those methods within your objects, else it'll throw an exception before you even start using it.
From what I see in the e.NewEvent sample code, they do still cast it to ManagementBaseObject.
They have to throw to the event as a ManagementBaseObject is because the NewEvent might be a ManagementObject or a ManagementClassObject, and you can't really determine whicch one it is. They didn't provide 2 seperate EventArrivedEventArg types like EventArrivedEventArgClassObject and EventArrivedEventArgObject should be because they didn't want to complicate things, and you should be able to do whatever you need within the boundaries of ManagementBaseObject.
It's actually correct design on their part, where it comes from a C++ background. What you should expect is the base class, because it is guaranteed from that object that all methods MUST and WILL have an implementation from the ManagementBaseObject.
Whereas if they followed the design above, it becomes cumbersome and needs to add more code as newer derived classes get created (if so).
A better way would be to use Interfaces, but this is Interop code, and interfaces aren't really fully supported.
Your case is similar to wanting to store multiple types of data in a collection class. You know which data is what type in the collection, but why does a collection class only able to return the base class? (don't talk about objects. Custom collections).
When you pass it into the collection, and cast it as a base class (to support all derived types in the collection), basically you lose the additional "data" from the derived class, and the derived class fits nicely into the base class structure, anything additional is chopped off and removed.
Obviously when you want to downcast it back to the derived type, you're not able to, because that additional "data" is already lost somewhere in data-dumping grounds.
That's one of the reasons why .NET and Java came up with boxing. Boxing casts it to an object class, which RETAINS these additional "data". Therefore you're able to unbox it again.
triplez wrote:When you pass it into the collection, and cast it as a base class (to support all derived types in the collection), basically you lose the additional "data" from the derived class, and the derived class fits nicely into the base class structure, anything additional is chopped off and removed.
Hmm, yes, sorry. My bad. I'm wrong. :P
Anyway, maybe you can try using "as" keyword... or for testing purposes, use "is" to test if it's from a derived type.
It might have done something to the type or something. LOL. :P