Home Dashboard Directory Help
Search

Type.IsAssignableFrom() handles boxing conversions inconsistently by TimwiTerby


Status: 

Closed
 as By Design Help for as By Design


4
0
Sign in
to vote
Type: Bug
ID: 600344
Opened: 9/17/2010 8:20:03 AM
Access Restriction: Public
0
Workaround(s)
view
1
User(s) can reproduce this bug

Description

The C# language specification, §6.1.7 Boxing conversions, says:

“A boxing conversion exists from a nullable-type to a reference type, if and only if a boxing conversion exists from the underlying non-nullable-value-type to the reference type.”

However:

    typeof(IFormattable).IsAssignableFrom(typeof(DateTime))     // True
    typeof(IFormattable).IsAssignableFrom(typeof(DateTime?))     // False
Details
Sign in to post a comment.
Posted by Microsoft on 1/7/2011 at 3:04 PM
Here is some additional information. When you assign a DateTime? (Nullable<DateTime>) value to a variable typed IFormattable, the C# compiler emits a box instruction before the real assignment. According to the CLI spec (Ecma 335), when a Nullable type is boxed, a boxed copy of the underlying type (in this case DateTime) is created rather than the enclosing Nullable type. So the assignment actually happens between DateTime (not DateTime?) and IFormattable. Nullable<DateTime> is not considered assignable to IFormattable.

Thanks,
Weitao
Microsoft Corp.
Posted by Microsoft on 1/4/2011 at 11:33 AM
We have reviewed your bug and have determined that the behavior that you described is by design. We are now archiving this issue. Thanks for using Visual Studio and the .Net Framework!

Type.IsAssignableFrom evaluates whether a location of a certain type can be assigned a value of a given type. If the location is of "DateTime?", it can be assigned either a DateTime or a null. On the other hand, if the location is a DateTime, you can only assign it a DateTime but not a "DateTime?" which is really a Nullable<DateTime>. This is different from "assignability" in some programming languates. But like most other reflection APIs, IsAssignableFrom reflects a type system concept, not a programming language one.

In short, a "DateTime?" value can be unboxed to a DateTime or an IFormattable value. But it cannot be assigned to a DateTime or IFormattable location.

Thanks,
Weitao Su
Microsoft Corp.
Posted by David A Nelson on 9/20/2010 at 9:45 AM
The method documentation clearly specifies the conditions under which it returns true:

"true if c and the current Type represent the same type, or if the current Type is in the inheritance hierarchy of c, or if the current Type is an interface that c implements, or if c is a generic type parameter and the current Type represents one of the constraints of c. false if none of these conditions are true, or if c is a null reference (Nothing in Visual Basic)."

Nothing is mentioned about boxing conversions.

http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx
Posted by Microsoft on 9/19/2010 at 7:03 PM
Thanks for your feedback. We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 9/17/2010 at 5:09 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.