System.OleAutBinder.ChangeType C# (CSharp) Method

ChangeType() public method

public ChangeType ( Object value, Type type, System.Globalization.CultureInfo cultureInfo ) : Object
value Object
type Type
cultureInfo System.Globalization.CultureInfo
return Object
        public override Object ChangeType(Object value, Type type, CultureInfo cultureInfo)
        {
            Variant myValue = new Variant(value);
            if (cultureInfo == null)
                cultureInfo = CultureInfo.CurrentCulture;
                
    #if DISPLAY_DEBUG_INFO      
            Console.Write("In OleAutBinder::ChangeType converting variant of type: ");
            Console.Write(myValue.VariantType);
            Console.Write(" to type: ");
            Console.WriteLine(type.Name);
    #endif      

            if (type.IsByRef)
            {
    #if DISPLAY_DEBUG_INFO      
                Console.WriteLine("Striping byref from the type to convert to.");
    #endif      
                type = type.GetElementType();
            }

            // If we are trying to convert from an object to another type then we don't
            // need the OLEAUT change type, we can just use the normal COM+ mechanisms.
            if (!type.IsPrimitive && type.IsInstanceOfType(value))
            {
    #if DISPLAY_DEBUG_INFO      
                Console.WriteLine("Source variant can be assigned to destination type");
    #endif      
                return value;
            }

            Type srcType = value.GetType();

            // Handle converting primitives to enums.
            if (type.IsEnum && srcType.IsPrimitive)
            {
    #if DISPLAY_DEBUG_INFO      
                Console.WriteLine("Converting primitive to enum");
    #endif      
                return Enum.Parse(type, value.ToString());
            }

            // Special case the convertion from DBNull.
            if (srcType == typeof(DBNull))
            {
                // The requested type is a DBNull so no convertion is required.            
                if (type == typeof(DBNull))
                    return value;

                // Visual J++ supported converting from DBNull to null so customers
                // have requested (via a CDCR) that DBNull be convertible to null.
                // We don't however allow this when converting to a value class, since null
                // doesn't make sense for these, or to object since this would change existing
                // semantics.               
                if ((type.IsClass && type != typeof(Object)) || type.IsInterface)
                    return null;
            }


            return base.ChangeType(value, type, cultureInfo);

        }