-Edit- Alternative question/example How do i cast A to object to class A when B can typcast to A?
I have class A, B, C. They all can implicitly convert to a string
public static implicit operator A(string sz_) { ... return sz; }
I have code that does this
object AClassWhichImplicitlyConvertsToString
{
...
((IKnownType)(String)AClassWhichImplicitlyConvertsToString).KnownFunc()
}
The problem is, AClassWhichImplicitlyConvertsToString isnt a string even though it can be typecast into one implicitly. I get a bad cast exception. How do i say its ok as long as the class has an operator to convert into a string?
There is almost certainly a better way of doing whatever it is you’re trying to do. If you provide more context, you’ll get more helpful answers.
If instead of (or as well as) making your classes implicitly covert to string you also give them a
ToStringoverride, you can then say:However, you’ll then get an exception on trying to cast a string into
KnownType. So I have to ask: why are you trying to go viastringin this situation? Casts are generally an ugly-ass thing that make you think “Maybe my design needs refactoring one day”. They’re not something you design into your class library as a recommended usage pattern. They’re a low-level facility with predictable behaviour, so there is no way (and no good reason to provide a way) to override what an explicit cast does.UpdateJudging from your comment you are mixing together runtime polymorphism and static (compile time) conversion. They don’t mix too well. Are you previously a user of dynamically typed languages? It seems like you might be. If you have a method:
Then the author of that method has no compile-time knowledge of what operations are available on
obj. So they can say:This then blows up at compile time for classes that aren’t
IFiddly. But in a statically typed language, you can say:This will blow up at compile time if the wrong kind of object is passed, and you don’t need to check anything at runtime. Less code, bugs found sooner… how neat is that?
The implicit conversion feature is part of the operator overloading set of features. These are all very much tied to static types. They are resolved at compile time based on the known type of the object. So if you don’t know the actual class of an object, there is no (built-in) way to call operators on it. It just doesn’t mix with dynamic typing.
If it is possible to get a string (such as a “name”) from an
IFiddlyobject, then you can make it a property on that interface:Or (as I noted before) you could just override
ToStringon any object, as that’s avirtualmethod on theobjectclass that all classes ultimately inherit from. So by saying:You are going to be calling the
ToStringimplementation defined in whatever classsomeObjectis an instance of.In summary: