示例#1
0
        public override void startElement(String elementName, NameValueCollection attributes)
        {
            if (elementName.Equals(XMLConstants.MG_TAG_ASSEMBLY))
            {
                String path                 = attributes[XMLConstants.MG_ATTR_ASSEMBLY_PATH];
                String fullName             = attributes[XMLConstants.MG_ATTR_FULLNAME];
                bool   useSpecific          = attributes[XMLConstants.MG_ATTR_ISSPECIFIC] != null && attributes[XMLConstants.MG_ATTR_ISSPECIFIC].Equals("1", StringComparison.CurrentCultureIgnoreCase);
                String content              = attributes[XMLConstants.MG_ATTR_ASSEMBLY_CONTENT];
                bool   isGuiThreadExecution = attributes[XMLConstants.MG_ATTR_IS_GUI_THREAD_EXECUTION] != null && attributes[XMLConstants.MG_ATTR_IS_GUI_THREAD_EXECUTION].Equals("1", StringComparison.CurrentCultureIgnoreCase);
                if (path != null)
                {
                    ReflectionServices.AddAssembly(fullName, useSpecific, path, isGuiThreadExecution);
                }
                else
                {
                    // Cache not used - the assembly code was passed
#if !PocketPC
                    ReflectionServices.AddAssembly(fullName, Base64.decodeToByte(content), isGuiThreadExecution);
#else
                    BinaryWriter bw      = new BinaryWriter(File.Open(fullName, FileMode.Create));
                    byte[]       decoded = Base64.decodeToByte(content);
                    if (decoded != null)
                    {
                        bw.Write(decoded);
                    }
                    bw.Close();
                    ReflectionServices.AddAssembly(fullName, false, fullName, isGuiThreadExecution);
#endif
                }
            }
        }
示例#2
0
        /// <summary>cast object from one type to an other</summary>
        /// <param name="o"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Object DynCast(Object o, Type type)
        {
            if (o == null)
            {
#if PocketPC
                return(Convert.ChangeType(o, type, null)); //TODO implement IFormatProvider for third parameter
#else
                return(Convert.ChangeType(o, type));
#endif
            }

            //support enum conversion
            if (type.IsEnum && o.GetType().IsPrimitive)
            {
                Type underlyingType = Enum.GetUnderlyingType(type);
                if (underlyingType != o.GetType())
                {
                    o = DynCast(o, underlyingType);
                }
                return(Enum.ToObject(type, o));
            }

            // 1. same type
            // 2. type is in the inheritance hierarchy of 'o'
            // 3. type is an interface that 'o' implements
            else if (type.IsAssignableFrom(o.GetType()))
            {
                return(o);
            }

            // implicit operator cast (if any)
            Object castedObj = ReflectionServices.ImplicitExplicitConvert(o, type, false);
            if (castedObj != null)
            {
                return(castedObj);
            }

            castedObj = ReflectionServices.ImplicitExplicitConvert(o, type, true);
            if (castedObj != null)
            {
                return(castedObj);
            }

            // common language runtime types (Boolean, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single,
            // Double, Decimal, DateTime, Char, and String) implements IConvertible for conversion.
            if (o is IConvertible)
            {
#if PocketPC
                return(Convert.ChangeType(o, type, null)); //TODO implement IFormatProvider for third parameter
#else
                return(Convert.ChangeType(o, type));
#endif
            }
            else
            {
                throw new Exception(string.Format("Unable to cast object of type '{0}' to type '{1}'.", o.GetType().Name, type.Name));
            }
        }