❔ cast parent to child

Have a problem about inheritance. I have an interface that are shared between a net framework project and net 6 project. Some of methods are called by passing classA object. My idea was to have classB inherits classA so that in the net framework project I can call the method by passing classA. And in the Net 6 project I can cast classA to classB (parent to child). classA is a class with only some enum and properties. classB have some methods that uses libraries which might not be compatible in net framework. However, I learnt from some places that parent to child is not allow Where some places says it is possible by using as and is keyword.
18 Replies
LPeter1997
LPeter19972y ago
A parent to child casting is possible If you are sure that it's the child type, you can use regular casts (Child)parent This will throw an exception at runtime, if it failed (because it wasn't actually a Child type) A safer way is to pattern match for it:
if (parent is Child child)
{
// Here child is usable and is of the Child type
}
if (parent is Child child)
{
// Here child is usable and is of the Child type
}
KKSK@🌸🎵🌸🎵🌸
So if I break this class into
KKSK@🌸🎵🌸🎵🌸
Where UnitAbnormalStateChangeData inherits UnitAbnormalStateChangeDataProperties And UnitAbnormalStateChangeData have the rest of the original methods
KKSK@🌸🎵🌸🎵🌸
Can I rewrite the above to the following?
KKSK@🌸🎵🌸🎵🌸
I just kind of understand your comment. What you mean is if object was child and was cast to parent, this can be cast to child again? But my case was the original is of a parent, and I am passing this parent object to this method. Within the method I am casting it to child. So it seems like impossible?
LPeter1997
LPeter19972y ago
If the instance is created as a parent, then no, there's no way to enforce it into being a child And I could never see a reason why one would want this
KKSK@🌸🎵🌸🎵🌸
Mainly because I want to separate this class so that I can pass the properties class instance to the method from the net framework project and call methods only in the Net 6 project.
LPeter1997
LPeter19972y ago
Then why not just pass the class
KKSK@🌸🎵🌸🎵🌸
The class was originally in the Net 6 project where the methods include some libraries that may not be compatible in the net framework class. I am currently implementing a shared library that uses an interface based grpc to communicate in between. So I am trying to move the class to the shared project. Originally everything was all tcp socket
LPeter1997
LPeter19972y ago
What version does the shared project target, and if it's an interface, what's the blocker?
KKSK@🌸🎵🌸🎵🌸
And manually parsing Net framework is the shared project target
LPeter1997
LPeter19972y ago
If the shared project targets Fx, you won't be able to reference it in the .NET 6 project
KKSK@🌸🎵🌸🎵🌸
Is actually not shared project as the one in VS is just a library created in net framework. And I referenced it I just called it a shared library because both project is referencing it I guess the only solution is an AutoMapper?
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.