Is there a way to do dynamic implicit type casting in C#?

Yes, yes you are. To be nit-picky, you should be saying "user-defined implicit conversion" rather than "implicit cast" -- a cast is (almost) always explicit. But your deduction that overload resolution chooses which user-defined conversion to call at compile time and not at run time is correct Is there some other way to do this?

Yes. In C# 4 if you type your "object" as "dynamic" then we start up the compiler again at runtime and re-perform all the analysis on the operands as though their compile-time types were the current run-time types As you might imagine, this is not cheap, though we are very smart about caching and re-using the results should you do this in a tight loop.

Yes, yes you are. To be nit-picky, you should be saying "user-defined implicit conversion" rather than "implicit cast" -- a cast is (almost) always explicit. But your deduction that overload resolution chooses which user-defined conversion to call at compile time and not at run time is correct.

Is there some other way to do this? Yes.In C# 4 if you type your "object" as "dynamic" then we start up the compiler again at runtime and re-perform all the analysis on the operands as though their compile-time types were the current run-time types. As you might imagine, this is not cheap, though we are very smart about caching and re-using the results should you do this in a tight loop.

Adding an explicit operator should work: msdn.microsoft.com/en-us/library/85w54y0...).aspx.

2 This is incorrect. – Eric Lippert Jan 19 '10 at 0:24.

Cannot implicitly convert type 'object' to 'MyDateTime'. Makes sense to me. This compiles fine.

Unable to cast object of type 'System. Int64' to type MyDateTime'. This tells me that C# implicit cast operators are applied at compile time only, and are not applied when the .

NET runtime is attempting to dynamically cast an object to another type. Am I correct? Is there some other way to do this?

By the way, the full application is that I'm using Delegate.DynamicInvoke() to call a function that takes a MyDateTime parameter, and the type of the argument I'm passing to DynamicInvoke is a long.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions