Simply curious, MSDN says that
Calling this method always throws InvalidCastException.
So why does this overload exist in the framework? Is it following some sort of pattern? I would assume for backwards compatibility it would be better to remove the overload and receive compile errors rather than run-time exceptions. I don't remember if this method was actuall开发者_StackOverflowy supported in previous frameworks though.
As you can see in the documentation in the link you provided:
Remarks
This method is reserved for future use.
They simply haven't found an implementation they are happy with or is necessary, but they are planning for it to be so.
I believe that IConvertable requires this to be implemented.
I guess it just calls DateTime.IConvertible.ToInt32 Method which is in turn responsible for throwing that exception. One day it will do the conversion and Convert.ToInt32(DateTime)
will work without any change!
They simply put it in the documentation, nothing special. If this does not exist, the same condition can be:
Convert.ToInt32(Object)
精彩评论