Generics: casting and value types, why is this illegal?

后端 未结 3 791
太阳男子
太阳男子 2020-11-29 09:46

Why is this a compile time error?

public TCastTo CastMe(TSource i)
{
     return (TCastTo)i;
}

Error:

相关标签:
3条回答
  • 2020-11-29 09:58

    The compile error is caused because TSource cannot be implicitly cast to TCastTo. The two types may share a branch on their inheritance tree, but there is no guarantee. If you wanted to call only types that did share an ancestor, you should modify the CastMe() signature to use the ancestor type instead of generics.

    The runtime error example avoids the error in your first example by first casting the TSource i to an object, something all objects in C# derive from. While the compiler doesn't complain (because object -> something that derives from it, could be valid), the behaviour of casting via (Type)variable syntax will throw if the cast is invalid. (The same problem that the compiler prevented from happening in example 1).

    Another solution, which does something similar to what you're looking for...

        public static T2 CastTo<T, T2>(T input, Func<T, T2> convert)
        {
            return convert(input);
        }
    

    You'd call it like this.

    int a = 314;
    long b = CastTo(a, i=>(long)i);
    

    Hopefully this helps.

    0 讨论(0)
  • 2020-11-29 10:21

    Why is this a compile time error?

    The problem is that every possible combination of value types has different rules for what a cast means. Casting a 64 bit double to a 16 bit int is completely different code from casting a decimal to a float, and so on. The number of possibilities is enormous. So think like the compiler. What code is the compiler supposed to generate for your program?

    The compiler would have to generate code that starts the compiler again at runtime, does a fresh analysis of the types, and dynamically emits the appropriate code.

    That seems like perhaps more work and less performance than you expected to get with generics, so we simply outlaw it. If what you really want is for the compiler to start up again and do an analysis of the types, use "dynamic" in C# 4; that's what it does.

    And why is this a runtime error?

    Same reason.

    A boxed int may only be unboxed to int (or int?), for the same reason as above; if the CLR tried to do every possible conversion from a boxed value type to every other possible value type then essentially it has to run a compiler again at runtime. That would be unexpectedly slow.

    So why is it not an error for reference types?

    Because every reference type conversion is the same as every other reference type conversion: you interrogate the object to see if it is derived from or identical to the desired type. If it's not, you throw an exception (if doing a cast) or result in null/false (if using the "as/is" operators). The rules are consistent for reference types in a way that they are not for value types. Remember reference types know their own type. Value types do not; with value types, the variable doing the storage is the only thing that knows the type semantics that apply to those bits. Value types contain their values and no additional information. Reference types contain their values plus lots of extra data.

    For more information see my article on the subject:

    http://ericlippert.com/2009/03/03/representation-and-identity/

    0 讨论(0)
  • 2020-11-29 10:24

    C# uses one cast syntax for multiple different underlying operations:

    • upcast
    • downcast
    • boxing
    • unboxing
    • numeric conversion
    • user-defined conversion

    In generic context, the compiler has no way of knowing which of those is correct, and they all generate different MSIL, so it bails out.

    By writing return (TCastTo)(object)i; instead, you force the compiler to do an upcast to object, followed by a downcast to TCastTo. The compiler will generate code, but if that wasn't the right way to convert the types in question, you'll get a runtime error.


    Code Sample:

    public static class DefaultConverter<TInput, TOutput>
    {
        private static Converter<TInput, TOutput> cached;
    
        static DefaultConverter()
        {
            ParameterExpression p = Expression.Parameter(typeof(TSource));
            cached = Expression.Lambda<Converter<TSource, TCastTo>(Expression.Convert(p, typeof(TCastTo), p).Compile();
        }
    
        public static Converter<TInput, TOutput> Instance { return cached; }
    }
    
    public static class DefaultConverter<TOutput>
    {
         public static TOutput ConvertBen<TInput>(TInput from) { return DefaultConverter<TInput, TOutput>.Instance.Invoke(from); }
         public static TOutput ConvertEric(dynamic from) { return from; }
    }
    

    Eric's way sure is shorter, but I think mine should be faster.

    0 讨论(0)
提交回复
热议问题