예제 #1
0
 internal static ConversionClass ClassifyPredefinedCLRConversion(Type TargetType, Type SourceType)
 {
     if (TargetType == SourceType)
     {
         return(ConversionClass.Identity);
     }
     if (Symbols.IsRootObjectType(TargetType) || Symbols.IsOrInheritsFrom(SourceType, TargetType))
     {
         return(ConversionClass.Widening);
     }
     if (Symbols.IsRootObjectType(SourceType) || Symbols.IsOrInheritsFrom(TargetType, SourceType))
     {
         return(ConversionClass.Narrowing);
     }
     if (Symbols.IsInterface(SourceType))
     {
         if ((Symbols.IsClass(TargetType) || Symbols.IsArrayType(TargetType)) || Symbols.IsGenericParameter(TargetType))
         {
             return(ConversionClass.Narrowing);
         }
         if (Symbols.IsInterface(TargetType))
         {
             return(ConversionClass.Narrowing);
         }
         if (!Symbols.IsValueType(TargetType))
         {
             return(ConversionClass.Narrowing);
         }
         if (Symbols.Implements(TargetType, SourceType))
         {
             return(ConversionClass.Narrowing);
         }
         return(ConversionClass.None);
     }
     if (Symbols.IsInterface(TargetType))
     {
         if (Symbols.IsArrayType(SourceType))
         {
             return(ClassifyCLRArrayToInterfaceConversion(TargetType, SourceType));
         }
         if (Symbols.IsValueType(SourceType))
         {
             if (Symbols.Implements(SourceType, TargetType))
             {
                 return(ConversionClass.Widening);
             }
             return(ConversionClass.None);
         }
         if (Symbols.IsClass(SourceType))
         {
             if (Symbols.Implements(SourceType, TargetType))
             {
                 return(ConversionClass.Widening);
             }
             return(ConversionClass.Narrowing);
         }
     }
     if (Symbols.IsEnum(SourceType) || Symbols.IsEnum(TargetType))
     {
         if (Symbols.GetTypeCode(SourceType) != Symbols.GetTypeCode(TargetType))
         {
             return(ConversionClass.None);
         }
         if (Symbols.IsEnum(TargetType))
         {
             return(ConversionClass.Narrowing);
         }
         return(ConversionClass.Widening);
     }
     if (Symbols.IsGenericParameter(SourceType))
     {
         if (!Symbols.IsClassOrInterface(TargetType))
         {
             return(ConversionClass.None);
         }
         foreach (Type type2 in Symbols.GetInterfaceConstraints(SourceType))
         {
             switch (ClassifyPredefinedConversion(TargetType, type2))
             {
             case ConversionClass.Widening:
             case ConversionClass.Identity:
                 return(ConversionClass.Widening);
             }
         }
         Type classConstraint = Symbols.GetClassConstraint(SourceType);
         if (classConstraint != null)
         {
             switch (ClassifyPredefinedConversion(TargetType, classConstraint))
             {
             case ConversionClass.Widening:
             case ConversionClass.Identity:
                 return(ConversionClass.Widening);
             }
         }
         return(Interaction.IIf <ConversionClass>(Symbols.IsInterface(TargetType), ConversionClass.Narrowing, ConversionClass.None));
     }
     if (Symbols.IsGenericParameter(TargetType))
     {
         Type derived = Symbols.GetClassConstraint(TargetType);
         if ((derived != null) && Symbols.IsOrInheritsFrom(derived, SourceType))
         {
             return(ConversionClass.Narrowing);
         }
         return(ConversionClass.None);
     }
     if ((Symbols.IsArrayType(SourceType) && Symbols.IsArrayType(TargetType)) && (SourceType.GetArrayRank() == TargetType.GetArrayRank()))
     {
         return(ClassifyCLRConversionForArrayElementTypes(TargetType.GetElementType(), SourceType.GetElementType()));
     }
     return(ConversionClass.None);
 }