Example #1
0
        public ResXDataNode(string name, object value, Func <Type, string> typeNameConverter)
        {
            if (name == null)
            {
                throw (new ArgumentNullException(nameof(name)));
            }
            if (name.Length == 0)
            {
                throw (new ArgumentException(nameof(name)));
            }

            this.typeNameConverter = typeNameConverter;

            Type valueType = (value == null) ? typeof(object) : value.GetType();

            if (value != null && !valueType.IsSerializable)
            {
                throw new InvalidOperationException(string.Format(SR.NotSerializableType, name, valueType.FullName));
            }
            if (value != null)
            {
                typeName = MultitargetUtil.GetAssemblyQualifiedName(valueType, this.typeNameConverter);
            }

            this.name  = name;
            this.value = value;
        }
Example #2
0
        internal DataNodeInfo GetDataNodeInfo()
        {
            bool shouldSerialize = true;

            if (nodeInfo != null)
            {
                shouldSerialize = false;
            }
            else
            {
                nodeInfo = new DataNodeInfo();
            }
            nodeInfo.Name    = Name;
            nodeInfo.Comment = Comment;

            // We always serialize if this node represents a FileRef. This is because FileRef is a public property,
            // so someone could have modified it.
            if (shouldSerialize || FileRefFullPath != null)
            {
                // if we dont have a datanodeinfo it could be either
                // a direct object OR a fileref
                if (FileRefFullPath != null)
                {
                    nodeInfo.ValueData = FileRef.ToString();
                    nodeInfo.MimeType  = null;
                    nodeInfo.TypeName  = MultitargetUtil.GetAssemblyQualifiedName(typeof(ResXFileRef), typeNameConverter);
                }
                else
                {
                    // serialize to string inside the nodeInfo
                    FillDataNodeInfoFromObject(nodeInfo, value);
                }
            }
            return(nodeInfo);
        }
        internal DataNodeInfo GetDataNodeInfo()
        {
            bool flag = true;

            if (this.nodeInfo != null)
            {
                flag = false;
            }
            else
            {
                this.nodeInfo = new DataNodeInfo();
            }
            this.nodeInfo.Name    = this.Name;
            this.nodeInfo.Comment = this.Comment;
            if (flag || (this.FileRefFullPath != null))
            {
                if (this.FileRefFullPath != null)
                {
                    this.nodeInfo.ValueData = this.FileRef.ToString();
                    this.nodeInfo.MimeType  = null;
                    this.nodeInfo.TypeName  = MultitargetUtil.GetAssemblyQualifiedName(typeof(ResXFileRef), this.typeNameConverter);
                }
                else
                {
                    this.FillDataNodeInfoFromObject(this.nodeInfo, this.value);
                }
            }
            return(this.nodeInfo);
        }
        /// <devdoc>
        ///     Adds a string resource to the resources.
        /// </devdoc>
        private void AddDataRow(string elementName, string name, string value)
        {
            // if it's a null string, set it here as a resxnullref
            string typeName =
                value == null
                    ? MultitargetUtil.GetAssemblyQualifiedName(typeof(ResXNullRef), this.typeNameConverter)
                    : null;

            AddDataRow(elementName, name, value, typeName, null, null);
        }
        private string TypeNameWithAssembly(Type type)
        {
            //



            string result = MultitargetUtil.GetAssemblyQualifiedName(type, this.typeNameConverter);

            return(result);
        }
 private void AddDataRow(string elementName, string name, string value)
 {
     if (value == null)
     {
         this.AddDataRow(elementName, name, value, MultitargetUtil.GetAssemblyQualifiedName(typeof(ResXNullRef), this.typeNameConverter), null, null);
     }
     else
     {
         this.AddDataRow(elementName, name, value, null, null, null);
     }
 }
Example #7
0
        public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
        {
            // Normally we don't change typeName when changing the target framework,
            // only assembly version or assembly name might change, thus we are setting
            // typeName only if it changed with the framework version.
            // If binder passes in a null, BinaryFormatter will use the original value or
            // for un-serializable types will redirect to another type.
            // For example:
            //
            // Encoding = Encoding.GetEncoding("shift_jis");
            // public Encoding Encoding { get; set; }
            // property type (Encoding) is abstract, but the value is instantiated to a specific class,
            // and should be serialized as a specific class in order to be able to instantiate the result.
            //
            // another example are singleton objects like DBNull.Value which are serialized by System.UnitySerializationHolder
            typeName = null;
            if (_typeNameConverter is not null)
            {
                string assemblyQualifiedTypeName = MultitargetUtil.GetAssemblyQualifiedName(serializedType, _typeNameConverter);
                if (!string.IsNullOrEmpty(assemblyQualifiedTypeName))
                {
                    int pos = assemblyQualifiedTypeName.IndexOf(',');
                    if (pos > 0 && pos < assemblyQualifiedTypeName.Length - 1)
                    {
                        assemblyName = assemblyQualifiedTypeName.Substring(pos + 1).TrimStart();
                        string newTypeName = assemblyQualifiedTypeName.Substring(0, pos);
                        if (!string.Equals(newTypeName, serializedType.FullName, StringComparison.InvariantCulture))
                        {
                            typeName = newTypeName;
                        }

                        return;
                    }
                }
            }

            base.BindToName(serializedType, out assemblyName, out typeName);
        }
 public ResXDataNode(string name, object value, Func <System.Type, string> typeNameConverter)
 {
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     if (name.Length == 0)
     {
         throw new ArgumentException("name");
     }
     this.typeNameConverter = typeNameConverter;
     System.Type type = (value == null) ? typeof(object) : value.GetType();
     if ((value != null) && !type.IsSerializable)
     {
         throw new InvalidOperationException(System.Windows.Forms.SR.GetString("NotSerializableType", new object[] { name, type.FullName }));
     }
     if (value != null)
     {
         this.typeName = MultitargetUtil.GetAssemblyQualifiedName(type, this.typeNameConverter);
     }
     this.name  = name;
     this.value = value;
 }
        private void AddDataRow(string elementName, string name, string value, string type, string mimeType, string comment)
        {
            if (hasBeenSaved)
            {
                throw new InvalidOperationException(SR.ResXResourceWriterSaved);
            }

            string alias = null;

            if (!string.IsNullOrEmpty(type) && elementName == DataStr)
            {
                string assemblyName = GetFullName(type);
                if (string.IsNullOrEmpty(assemblyName))
                {
                    try {
                        Type typeObject = Type.GetType(type);
                        if (typeObject == typeof(string))
                        {
                            type = null;
                        }
                        else if (typeObject != null)
                        {
                            assemblyName = GetFullName(MultitargetUtil.GetAssemblyQualifiedName(typeObject, this.typeNameConverter));
                            alias        = GetAliasFromName(new AssemblyName(assemblyName));
                        }
                    } catch {
                    }
                }
                else
                {
                    alias = GetAliasFromName(new AssemblyName(GetFullName(type)));
                }
                //AddAssemblyRow(AssemblyStr, alias, GetFullName(type));
            }

            Writer.WriteStartElement(elementName); {
                Writer.WriteAttributeString(NameStr, name);

                if (!string.IsNullOrEmpty(alias) && !string.IsNullOrEmpty(type) && elementName == DataStr)
                {
                    // CHANGE: we still output version information. This might have
                    // to change in 3.2
                    string typeName  = GetTypeName(type);
                    string typeValue = typeName + ", " + alias;
                    Writer.WriteAttributeString(TypeStr, typeValue);
                }
                else
                {
                    if (type != null)
                    {
                        Writer.WriteAttributeString(TypeStr, type);
                    }
                }

                if (mimeType != null)
                {
                    Writer.WriteAttributeString(MimeTypeStr, mimeType);
                }

                if ((type == null && mimeType == null) || (type != null && type.StartsWith("System.Char", StringComparison.Ordinal)))
                {
                    Writer.WriteAttributeString("xml", "space", null, "preserve");
                }

                Writer.WriteStartElement(ValueStr); {
                    if (!string.IsNullOrEmpty(value))
                    {
                        Writer.WriteString(value);
                    }
                }
                Writer.WriteEndElement();
                if (!string.IsNullOrEmpty(comment))
                {
                    Writer.WriteStartElement(CommentStr); {
                        Writer.WriteString(comment);
                    }
                    Writer.WriteEndElement();
                }
            }
            Writer.WriteEndElement();
        }
        private void InitializeWriter()
        {
            if (xmlTextWriter == null)
            {
                //

                bool writeHeaderRequired = false;

                if (textWriter != null)
                {
                    textWriter.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                    writeHeaderRequired = true;

                    xmlTextWriter = new XmlTextWriter(textWriter);
                }
                else if (stream != null)
                {
                    xmlTextWriter = new XmlTextWriter(stream, System.Text.Encoding.UTF8);
                }
                else
                {
                    Debug.Assert(fileName != null, "Nothing to output to");
                    xmlTextWriter = new XmlTextWriter(fileName, System.Text.Encoding.UTF8);
                }
                xmlTextWriter.Formatting  = Formatting.Indented;
                xmlTextWriter.Indentation = 2;

                if (!writeHeaderRequired)
                {
                    xmlTextWriter.WriteStartDocument(); // writes <?xml version="1.0" encoding="utf-8"?>
                }
            }
            else
            {
                xmlTextWriter.WriteStartDocument();
            }

            xmlTextWriter.WriteStartElement("root");
            XmlTextReader reader = new XmlTextReader(new StringReader(ResourceSchema));

            reader.WhitespaceHandling = WhitespaceHandling.None;
            xmlTextWriter.WriteNode(reader, true);

            xmlTextWriter.WriteStartElement(ResHeaderStr); {
                xmlTextWriter.WriteAttributeString(NameStr, ResMimeTypeStr);
                xmlTextWriter.WriteStartElement(ValueStr); {
                    xmlTextWriter.WriteString(ResMimeType);
                }
                xmlTextWriter.WriteEndElement();
            }
            xmlTextWriter.WriteEndElement();
            xmlTextWriter.WriteStartElement(ResHeaderStr); {
                xmlTextWriter.WriteAttributeString(NameStr, VersionStr);
                xmlTextWriter.WriteStartElement(ValueStr); {
                    xmlTextWriter.WriteString(Version);
                }
                xmlTextWriter.WriteEndElement();
            }
            xmlTextWriter.WriteEndElement();
            xmlTextWriter.WriteStartElement(ResHeaderStr); {
                xmlTextWriter.WriteAttributeString(NameStr, ReaderStr);
                xmlTextWriter.WriteStartElement(ValueStr); {
                    xmlTextWriter.WriteString(MultitargetUtil.GetAssemblyQualifiedName(typeof(ResXResourceReader), this.typeNameConverter));
                }
                xmlTextWriter.WriteEndElement();
            }
            xmlTextWriter.WriteEndElement();
            xmlTextWriter.WriteStartElement(ResHeaderStr); {
                xmlTextWriter.WriteAttributeString(NameStr, WriterStr);
                xmlTextWriter.WriteStartElement(ValueStr); {
                    xmlTextWriter.WriteString(MultitargetUtil.GetAssemblyQualifiedName(typeof(ResXResourceWriter), this.typeNameConverter));
                }
                xmlTextWriter.WriteEndElement();
            }
            xmlTextWriter.WriteEndElement();

            initialized = true;
        }
        private object GenerateObjectFromDataNodeInfo(DataNodeInfo dataNodeInfo, ITypeResolutionService typeResolver)
        {
            object obj2     = null;
            string mimeType = dataNodeInfo.MimeType;
            string typeName = ((dataNodeInfo.TypeName == null) || (dataNodeInfo.TypeName.Length == 0)) ? MultitargetUtil.GetAssemblyQualifiedName(typeof(string), this.typeNameConverter) : dataNodeInfo.TypeName;

            if ((mimeType != null) && (mimeType.Length > 0))
            {
                if ((string.Equals(mimeType, ResXResourceWriter.BinSerializedObjectMimeType) || string.Equals(mimeType, ResXResourceWriter.Beta2CompatSerializedObjectMimeType)) || string.Equals(mimeType, ResXResourceWriter.CompatBinSerializedObjectMimeType))
                {
                    byte[] buffer = FromBase64WrappedString(dataNodeInfo.ValueData);
                    if (this.binaryFormatter == null)
                    {
                        this.binaryFormatter        = new BinaryFormatter();
                        this.binaryFormatter.Binder = new ResXSerializationBinder(typeResolver);
                    }
                    IFormatter binaryFormatter = this.binaryFormatter;
                    if ((buffer != null) && (buffer.Length > 0))
                    {
                        obj2 = binaryFormatter.Deserialize(new MemoryStream(buffer));
                        if (obj2 is ResXNullRef)
                        {
                            obj2 = null;
                        }
                    }
                    return(obj2);
                }
                if (string.Equals(mimeType, ResXResourceWriter.SoapSerializedObjectMimeType) || string.Equals(mimeType, ResXResourceWriter.CompatSoapSerializedObjectMimeType))
                {
                    byte[] buffer2 = FromBase64WrappedString(dataNodeInfo.ValueData);
                    if ((buffer2 != null) && (buffer2.Length > 0))
                    {
                        obj2 = this.CreateSoapFormatter().Deserialize(new MemoryStream(buffer2));
                        if (obj2 is ResXNullRef)
                        {
                            obj2 = null;
                        }
                    }
                    return(obj2);
                }
                if ((!string.Equals(mimeType, ResXResourceWriter.ByteArraySerializedObjectMimeType) || (typeName == null)) || (typeName.Length <= 0))
                {
                    return(obj2);
                }
                System.Type type = this.ResolveType(typeName, typeResolver);
                if (type != null)
                {
                    TypeConverter converter = TypeDescriptor.GetConverter(type);
                    if (converter.CanConvertFrom(typeof(byte[])))
                    {
                        byte[] buffer3 = FromBase64WrappedString(dataNodeInfo.ValueData);
                        if (buffer3 != null)
                        {
                            obj2 = converter.ConvertFrom(buffer3);
                        }
                    }
                    return(obj2);
                }
                string            str6       = System.Windows.Forms.SR.GetString("TypeLoadException", new object[] { typeName, dataNodeInfo.ReaderPosition.Y, dataNodeInfo.ReaderPosition.X });
                XmlException      exception  = new XmlException(str6, null, dataNodeInfo.ReaderPosition.Y, dataNodeInfo.ReaderPosition.X);
                TypeLoadException exception2 = new TypeLoadException(str6, exception);
                throw exception2;
            }
            if ((typeName == null) || (typeName.Length <= 0))
            {
                return(obj2);
            }
            System.Type type2 = this.ResolveType(typeName, typeResolver);
            if (type2 != null)
            {
                if (type2 == typeof(ResXNullRef))
                {
                    return(null);
                }
                if ((typeName.IndexOf("System.Byte[]") != -1) && (typeName.IndexOf("mscorlib") != -1))
                {
                    return(FromBase64WrappedString(dataNodeInfo.ValueData));
                }
                TypeConverter converter2 = TypeDescriptor.GetConverter(type2);
                if (!converter2.CanConvertFrom(typeof(string)))
                {
                    return(obj2);
                }
                string valueData = dataNodeInfo.ValueData;
                try
                {
                    return(converter2.ConvertFromInvariantString(valueData));
                }
                catch (NotSupportedException exception3)
                {
                    string                str8           = System.Windows.Forms.SR.GetString("NotSupported", new object[] { typeName, dataNodeInfo.ReaderPosition.Y, dataNodeInfo.ReaderPosition.X, exception3.Message });
                    XmlException          innerException = new XmlException(str8, exception3, dataNodeInfo.ReaderPosition.Y, dataNodeInfo.ReaderPosition.X);
                    NotSupportedException exception5     = new NotSupportedException(str8, innerException);
                    throw exception5;
                }
            }
            string            message    = System.Windows.Forms.SR.GetString("TypeLoadException", new object[] { typeName, dataNodeInfo.ReaderPosition.Y, dataNodeInfo.ReaderPosition.X });
            XmlException      inner      = new XmlException(message, null, dataNodeInfo.ReaderPosition.Y, dataNodeInfo.ReaderPosition.X);
            TypeLoadException exception7 = new TypeLoadException(message, inner);

            throw exception7;
        }
        private void AddDataRow(string elementName, string name, string value, string type, string mimeType, string comment)
        {
            if (this.hasBeenSaved)
            {
                throw new InvalidOperationException(System.Windows.Forms.SR.GetString("ResXResourceWriterSaved"));
            }
            string aliasFromName = null;

            if (!string.IsNullOrEmpty(type) && (elementName == "data"))
            {
                if (string.IsNullOrEmpty(this.GetFullName(type)))
                {
                    try
                    {
                        System.Type type2 = System.Type.GetType(type);
                        if (type2 == typeof(string))
                        {
                            type = null;
                        }
                        else if (type2 != null)
                        {
                            string fullName = this.GetFullName(MultitargetUtil.GetAssemblyQualifiedName(type2, this.typeNameConverter));
                            aliasFromName = this.GetAliasFromName(new AssemblyName(fullName));
                        }
                    }
                    catch
                    {
                    }
                }
                else
                {
                    aliasFromName = this.GetAliasFromName(new AssemblyName(this.GetFullName(type)));
                }
            }
            this.Writer.WriteStartElement(elementName);
            this.Writer.WriteAttributeString("name", name);
            if ((!string.IsNullOrEmpty(aliasFromName) && !string.IsNullOrEmpty(type)) && (elementName == "data"))
            {
                string str4 = this.GetTypeName(type) + ", " + aliasFromName;
                this.Writer.WriteAttributeString("type", str4);
            }
            else if (type != null)
            {
                this.Writer.WriteAttributeString("type", type);
            }
            if (mimeType != null)
            {
                this.Writer.WriteAttributeString("mimetype", mimeType);
            }
            if (((type == null) && (mimeType == null)) || ((type != null) && type.StartsWith("System.Char", StringComparison.Ordinal)))
            {
                this.Writer.WriteAttributeString("xml", "space", null, "preserve");
            }
            this.Writer.WriteStartElement("value");
            if (!string.IsNullOrEmpty(value))
            {
                this.Writer.WriteString(value);
            }
            this.Writer.WriteEndElement();
            if (!string.IsNullOrEmpty(comment))
            {
                this.Writer.WriteStartElement("comment");
                this.Writer.WriteString(comment);
                this.Writer.WriteEndElement();
            }
            this.Writer.WriteEndElement();
        }
Example #13
0
        private void ParseXml(XmlTextReader reader)
        {
            bool success = false;

            try
            {
                try
                {
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            string s = reader.LocalName;

                            if (reader.LocalName.Equals(ResXResourceWriter.AssemblyStr))
                            {
                                ParseAssemblyNode(reader);
                            }
                            else if (reader.LocalName.Equals(ResXResourceWriter.DataStr))
                            {
                                ParseDataNode(reader, false);
                            }
                            else if (reader.LocalName.Equals(ResXResourceWriter.ResHeaderStr))
                            {
                                ParseResHeaderNode(reader);
                            }
                            else if (reader.LocalName.Equals(ResXResourceWriter.MetadataStr))
                            {
                                ParseDataNode(reader, true);
                            }
                        }
                    }

                    success = true;
                }
                catch (SerializationException se)
                {
                    Point                  pt         = GetPosition(reader);
                    string                 newMessage = string.Format(SR.SerializationException, reader[ResXResourceWriter.TypeStr], pt.Y, pt.X, se.Message);
                    XmlException           xml        = new XmlException(newMessage, se, pt.Y, pt.X);
                    SerializationException newSe      = new SerializationException(newMessage, xml);

                    throw newSe;
                }
                catch (TargetInvocationException tie)
                {
                    Point        pt                  = GetPosition(reader);
                    string       newMessage          = string.Format(SR.InvocationException, reader[ResXResourceWriter.TypeStr], pt.Y, pt.X, tie.InnerException.Message);
                    XmlException xml                 = new XmlException(newMessage, tie.InnerException, pt.Y, pt.X);
                    TargetInvocationException newTie = new TargetInvocationException(newMessage, xml);

                    throw newTie;
                }
                catch (XmlException e)
                {
                    throw new ArgumentException(string.Format(SR.InvalidResXFile, e.Message), e);
                }
                catch (Exception e)
                {
                    if (MultitargetUtil.IsCriticalException(e))
                    {
                        throw;
                    }
                    else
                    {
                        Point        pt    = GetPosition(reader);
                        XmlException xmlEx = new XmlException(e.Message, e, pt.Y, pt.X);
                        throw new ArgumentException(string.Format(SR.InvalidResXFile, xmlEx.Message), xmlEx);
                    }
                }
            }
            finally
            {
                if (!success)
                {
                    _resData     = null;
                    _resMetadata = null;
                }
            }

            bool validFile = false;

            if (_resHeaderMimeType == ResXResourceWriter.ResMimeType)
            {
                Type readerType = typeof(ResXResourceReader);
                Type writerType = typeof(ResXResourceWriter);

                string readerTypeName = _resHeaderReaderType;
                string writerTypeName = _resHeaderWriterType;
                if (readerTypeName != null && readerTypeName.IndexOf(',') != -1)
                {
                    readerTypeName = readerTypeName.Split(',')[0].Trim();
                }
                if (writerTypeName != null && writerTypeName.IndexOf(',') != -1)
                {
                    writerTypeName = writerTypeName.Split(',')[0].Trim();
                }

                if (readerTypeName != null &&
                    writerTypeName != null &&
                    readerTypeName.Equals(readerType.FullName) &&
                    writerTypeName.Equals(writerType.FullName))
                {
                    validFile = true;
                }
            }

            if (!validFile)
            {
                _resData     = null;
                _resMetadata = null;
                throw new ArgumentException(SR.InvalidResXFileReaderWriterTypes);
            }
        }
        public string GetValueTypeName(ITypeResolutionService typeResolver)
        {
            if ((this.typeName != null) && (this.typeName.Length > 0))
            {
                if (this.typeName.Equals(MultitargetUtil.GetAssemblyQualifiedName(typeof(ResXNullRef), this.typeNameConverter)))
                {
                    return(MultitargetUtil.GetAssemblyQualifiedName(typeof(object), this.typeNameConverter));
                }
                return(this.typeName);
            }
            string fileRefType = this.FileRefType;

            System.Type type = null;
            if (fileRefType != null)
            {
                type = this.ResolveType(this.FileRefType, typeResolver);
            }
            else if (this.nodeInfo != null)
            {
                fileRefType = this.nodeInfo.TypeName;
                if ((fileRefType == null) || (fileRefType.Length == 0))
                {
                    if ((this.nodeInfo.MimeType != null) && (this.nodeInfo.MimeType.Length > 0))
                    {
                        object obj2 = null;
                        try
                        {
                            obj2 = this.GenerateObjectFromDataNodeInfo(this.nodeInfo, typeResolver);
                        }
                        catch (Exception exception)
                        {
                            if (System.Windows.Forms.ClientUtils.IsCriticalException(exception))
                            {
                                throw;
                            }
                            fileRefType = MultitargetUtil.GetAssemblyQualifiedName(typeof(object), this.typeNameConverter);
                        }
                        if (obj2 != null)
                        {
                            fileRefType = MultitargetUtil.GetAssemblyQualifiedName(obj2.GetType(), this.typeNameConverter);
                        }
                    }
                    else
                    {
                        fileRefType = MultitargetUtil.GetAssemblyQualifiedName(typeof(string), this.typeNameConverter);
                    }
                }
                else
                {
                    type = this.ResolveType(this.nodeInfo.TypeName, typeResolver);
                }
            }
            if (type == null)
            {
                return(fileRefType);
            }
            if (type == typeof(ResXNullRef))
            {
                return(MultitargetUtil.GetAssemblyQualifiedName(typeof(object), this.typeNameConverter));
            }
            return(MultitargetUtil.GetAssemblyQualifiedName(type, this.typeNameConverter));
        }
Example #15
0
        private void FillDataNodeInfoFromObject(DataNodeInfo nodeInfo, object value)
        {
            if (value is CultureInfo ci)
            { // special-case CultureInfo, cannot use CultureInfoConverter for serialization
                nodeInfo.ValueData = ci.Name;
                nodeInfo.TypeName  = MultitargetUtil.GetAssemblyQualifiedName(typeof(CultureInfo), typeNameConverter);
            }
            else if (value is string str)
            {
                nodeInfo.ValueData = str;
            }
            else if (value is byte[] bytes)
            {
                nodeInfo.ValueData = ToBase64WrappedString(bytes);
                nodeInfo.TypeName  = MultitargetUtil.GetAssemblyQualifiedName(typeof(byte[]), typeNameConverter);
            }
            else
            {
                Type valueType = (value == null) ? typeof(object) : value.GetType();
                if (value != null && !valueType.IsSerializable)
                {
                    throw new InvalidOperationException(string.Format(SR.NotSerializableType, name, valueType.FullName));
                }
                TypeConverter tc         = TypeDescriptor.GetConverter(valueType);
                bool          toString   = tc.CanConvertTo(typeof(string));
                bool          fromString = tc.CanConvertFrom(typeof(string));
                try
                {
                    if (toString && fromString)
                    {
                        nodeInfo.ValueData = tc.ConvertToInvariantString(value);
                        nodeInfo.TypeName  = MultitargetUtil.GetAssemblyQualifiedName(valueType, typeNameConverter);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    // Some custom type converters will throw in ConvertTo(string)
                    // to indicate that this object should be serialized through ISeriazable
                    // instead of as a string. This is semi-wrong, but something we will have to
                    // live with to allow user created Cursors to be serializable.
                    if (ClientUtils.IsSecurityOrCriticalException(ex))
                    {
                        throw;
                    }
                }

                bool toByteArray   = tc.CanConvertTo(typeof(byte[]));
                bool fromByteArray = tc.CanConvertFrom(typeof(byte[]));
                if (toByteArray && fromByteArray)
                {
                    byte[] data = (byte[])tc.ConvertTo(value, typeof(byte[]));
                    nodeInfo.ValueData = ToBase64WrappedString(data);
                    nodeInfo.MimeType  = ResXResourceWriter.ByteArraySerializedObjectMimeType;
                    nodeInfo.TypeName  = MultitargetUtil.GetAssemblyQualifiedName(valueType, typeNameConverter);
                    return;
                }

                if (value == null)
                {
                    nodeInfo.ValueData = string.Empty;
                    nodeInfo.TypeName  = MultitargetUtil.GetAssemblyQualifiedName(typeof(ResXNullRef), typeNameConverter);
                }
                else
                {
                    if (binaryFormatter == null)
                    {
                        binaryFormatter = new BinaryFormatter
                        {
                            Binder = new ResXSerializationBinder(typeNameConverter)
                        };
                    }

                    using (MemoryStream ms = new MemoryStream())
                    {
                        binaryFormatter.Serialize(ms, value);
                        nodeInfo.ValueData = ToBase64WrappedString(ms.ToArray());
                    }

                    nodeInfo.MimeType = ResXResourceWriter.DefaultSerializedObjectMimeType;
                }
            }
        }
        private void InitializeWriter()
        {
            if (this.xmlTextWriter == null)
            {
                bool flag = false;
                if (this.textWriter != null)
                {
                    this.textWriter.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                    flag = true;
                    this.xmlTextWriter = new XmlTextWriter(this.textWriter);
                }
                else if (this.stream != null)
                {
                    this.xmlTextWriter = new XmlTextWriter(this.stream, Encoding.UTF8);
                }
                else
                {
                    this.xmlTextWriter = new XmlTextWriter(this.fileName, Encoding.UTF8);
                }
                this.xmlTextWriter.Formatting  = Formatting.Indented;
                this.xmlTextWriter.Indentation = 2;
                if (!flag)
                {
                    this.xmlTextWriter.WriteStartDocument();
                }
            }
            else
            {
                this.xmlTextWriter.WriteStartDocument();
            }
            this.xmlTextWriter.WriteStartElement("root");
            XmlTextReader reader = new XmlTextReader(new StringReader(ResourceSchema))
            {
                WhitespaceHandling = WhitespaceHandling.None
            };

            this.xmlTextWriter.WriteNode(reader, true);
            this.xmlTextWriter.WriteStartElement("resheader");
            this.xmlTextWriter.WriteAttributeString("name", "resmimetype");
            this.xmlTextWriter.WriteStartElement("value");
            this.xmlTextWriter.WriteString(ResMimeType);
            this.xmlTextWriter.WriteEndElement();
            this.xmlTextWriter.WriteEndElement();
            this.xmlTextWriter.WriteStartElement("resheader");
            this.xmlTextWriter.WriteAttributeString("name", "version");
            this.xmlTextWriter.WriteStartElement("value");
            this.xmlTextWriter.WriteString(Version);
            this.xmlTextWriter.WriteEndElement();
            this.xmlTextWriter.WriteEndElement();
            this.xmlTextWriter.WriteStartElement("resheader");
            this.xmlTextWriter.WriteAttributeString("name", "reader");
            this.xmlTextWriter.WriteStartElement("value");
            this.xmlTextWriter.WriteString(MultitargetUtil.GetAssemblyQualifiedName(typeof(ResXResourceReader), this.typeNameConverter));
            this.xmlTextWriter.WriteEndElement();
            this.xmlTextWriter.WriteEndElement();
            this.xmlTextWriter.WriteStartElement("resheader");
            this.xmlTextWriter.WriteAttributeString("name", "writer");
            this.xmlTextWriter.WriteStartElement("value");
            this.xmlTextWriter.WriteString(MultitargetUtil.GetAssemblyQualifiedName(typeof(ResXResourceWriter), this.typeNameConverter));
            this.xmlTextWriter.WriteEndElement();
            this.xmlTextWriter.WriteEndElement();
            this.initialized = true;
        }
Example #17
0
        private object GenerateObjectFromDataNodeInfo(DataNodeInfo dataNodeInfo, ITypeResolutionService typeResolver)
        {
            object result       = null;
            string mimeTypeName = dataNodeInfo.MimeType;
            // default behavior: if we dont have a type name, it's a string
            string typeName =
                string.IsNullOrEmpty(dataNodeInfo.TypeName)
                    ? MultitargetUtil.GetAssemblyQualifiedName(typeof(string), typeNameConverter)
                    : dataNodeInfo.TypeName;

            if (!string.IsNullOrEmpty(mimeTypeName))
            {
                if (string.Equals(mimeTypeName, ResXResourceWriter.BinSerializedObjectMimeType) ||
                    string.Equals(mimeTypeName, ResXResourceWriter.Beta2CompatSerializedObjectMimeType) ||
                    string.Equals(mimeTypeName, ResXResourceWriter.CompatBinSerializedObjectMimeType))
                {
                    string text           = dataNodeInfo.ValueData;
                    byte[] serializedData = FromBase64WrappedString(text);

                    if (binaryFormatter == null)
                    {
                        binaryFormatter = new BinaryFormatter
                        {
                            Binder = new ResXSerializationBinder(typeResolver)
                        };
                    }
                    IFormatter formatter = binaryFormatter;
                    if (serializedData != null && serializedData.Length > 0)
                    {
                        result = formatter.Deserialize(new MemoryStream(serializedData));
                        if (result is ResXNullRef)
                        {
                            result = null;
                        }
                    }
                }

                else if (string.Equals(mimeTypeName, ResXResourceWriter.ByteArraySerializedObjectMimeType))
                {
                    if (!string.IsNullOrEmpty(typeName))
                    {
                        Type type = ResolveType(typeName, typeResolver);
                        if (type != null)
                        {
                            TypeConverter tc = TypeDescriptor.GetConverter(type);
                            if (tc.CanConvertFrom(typeof(byte[])))
                            {
                                string text           = dataNodeInfo.ValueData;
                                byte[] serializedData = FromBase64WrappedString(text);

                                if (serializedData != null)
                                {
                                    result = tc.ConvertFrom(serializedData);
                                }
                            }
                        }
                        else
                        {
                            string            newMessage = string.Format(SR.TypeLoadException, typeName, dataNodeInfo.ReaderPosition.Y, dataNodeInfo.ReaderPosition.X);
                            XmlException      xml        = new XmlException(newMessage, null, dataNodeInfo.ReaderPosition.Y, dataNodeInfo.ReaderPosition.X);
                            TypeLoadException newTle     = new TypeLoadException(newMessage, xml);

                            throw newTle;
                        }
                    }
                }
            }
            else if (!string.IsNullOrEmpty(typeName))
            {
                Type type = ResolveType(typeName, typeResolver);
                if (type != null)
                {
                    if (type == typeof(ResXNullRef))
                    {
                        result = null;
                    }
                    else if (typeName.IndexOf("System.Byte[]") != -1 && typeName.IndexOf("mscorlib") != -1)
                    {
                        // Handle byte[]'s, which are stored as base-64 encoded strings.
                        // We can't hard-code byte[] type name due to version number
                        // updates & potential whitespace issues with ResX files.
                        result = FromBase64WrappedString(dataNodeInfo.ValueData);
                    }
                    else
                    {
                        TypeConverter tc = TypeDescriptor.GetConverter(type);
                        if (tc.CanConvertFrom(typeof(string)))
                        {
                            string text = dataNodeInfo.ValueData;
                            try
                            {
                                result = tc.ConvertFromInvariantString(text);
                            }
                            catch (NotSupportedException nse)
                            {
                                string                newMessage = string.Format(SR.NotSupported, typeName, dataNodeInfo.ReaderPosition.Y, dataNodeInfo.ReaderPosition.X, nse.Message);
                                XmlException          xml        = new XmlException(newMessage, nse, dataNodeInfo.ReaderPosition.Y, dataNodeInfo.ReaderPosition.X);
                                NotSupportedException newNse     = new NotSupportedException(newMessage, xml);
                                throw newNse;
                            }
                        }
                        else
                        {
                            Debug.WriteLine("Converter for " + type.FullName + " doesn't support string conversion");
                        }
                    }
                }
                else
                {
                    string            newMessage = string.Format(SR.TypeLoadException, typeName, dataNodeInfo.ReaderPosition.Y, dataNodeInfo.ReaderPosition.X);
                    XmlException      xml        = new XmlException(newMessage, null, dataNodeInfo.ReaderPosition.Y, dataNodeInfo.ReaderPosition.X);
                    TypeLoadException newTle     = new TypeLoadException(newMessage, xml);

                    throw newTle;
                }
            }
            else
            {
                // if mimeTypeName and typeName are not filled in, the value must be a string
                Debug.Assert(value is string, "Resource entries with no Type or MimeType must be encoded as strings");
            }
            return(result);
        }
 private string TypeNameWithAssembly(System.Type type)
 {
     return(MultitargetUtil.GetAssemblyQualifiedName(type, this.typeNameConverter));
 }
Example #19
0
        /// <summary>
        ///    Get the FQ type name for this datanode.
        ///    We return typeof(object) for ResXNullRef
        /// </summary>
        public string GetValueTypeName(ITypeResolutionService typeResolver)
        {
            // the type name here is always a FQN
            if (!string.IsNullOrEmpty(typeName))
            {
                return
                    (typeName == MultitargetUtil.GetAssemblyQualifiedName(typeof(ResXNullRef), typeNameConverter)
                        ? MultitargetUtil.GetAssemblyQualifiedName(typeof(object), typeNameConverter)
                        : typeName);
            }
            string result     = FileRefType;
            Type   objectType = null;

            // do we have a fileref?
            if (result != null)
            {
                // try to resolve this type
                objectType = ResolveType(FileRefType, typeResolver);
            }
            else if (nodeInfo != null)
            {
                // we dont have a fileref, try to resolve the type of the datanode
                result = nodeInfo.TypeName;
                // if typename is null, the default is just a string
                if (string.IsNullOrEmpty(result))
                {
                    // we still dont know... do we have a mimetype? if yes, our only option is to
                    // deserialize to know what we're dealing with... very inefficient...
                    if (!string.IsNullOrEmpty(nodeInfo.MimeType))
                    {
                        object insideObject = null;

                        try
                        {
                            insideObject = GenerateObjectFromDataNodeInfo(nodeInfo, typeResolver);
                        }
                        catch (Exception ex)
                        { // it'd be better to catch SerializationException but the underlying type resolver
                          // can throw things like FileNotFoundException which is kinda confusing, so I am catching all here..
                            if (ClientUtils.IsCriticalException(ex))
                            {
                                throw;
                            }
                            // something went wrong, type is not specified at all or stream is corrupted
                            // return system.object
                            result = MultitargetUtil.GetAssemblyQualifiedName(typeof(object), typeNameConverter);
                        }

                        if (insideObject != null)
                        {
                            result = MultitargetUtil.GetAssemblyQualifiedName(insideObject.GetType(), typeNameConverter);
                        }
                    }
                    else
                    {
                        // no typename, no mimetype, we have a string...
                        result = MultitargetUtil.GetAssemblyQualifiedName(typeof(string), typeNameConverter);
                    }
                }
                else
                {
                    objectType = ResolveType(nodeInfo.TypeName, typeResolver);
                }
            }
            if (objectType != null)
            {
                if (objectType == typeof(ResXNullRef))
                {
                    result = MultitargetUtil.GetAssemblyQualifiedName(typeof(object), typeNameConverter);
                }
                else
                {
                    result = MultitargetUtil.GetAssemblyQualifiedName(objectType, typeNameConverter);
                }
            }
            return(result);
        }
        private void FillDataNodeInfoFromObject(DataNodeInfo nodeInfo, object value)
        {
            CultureInfo info = value as CultureInfo;

            if (info != null)
            {
                nodeInfo.ValueData = info.Name;
                nodeInfo.TypeName  = MultitargetUtil.GetAssemblyQualifiedName(typeof(CultureInfo), this.typeNameConverter);
            }
            else if (value is string)
            {
                nodeInfo.ValueData = (string)value;
                if (value == null)
                {
                    nodeInfo.TypeName = MultitargetUtil.GetAssemblyQualifiedName(typeof(ResXNullRef), this.typeNameConverter);
                }
            }
            else if (value is byte[])
            {
                nodeInfo.ValueData = ToBase64WrappedString((byte[])value);
                nodeInfo.TypeName  = MultitargetUtil.GetAssemblyQualifiedName(typeof(byte[]), this.typeNameConverter);
            }
            else
            {
                System.Type type = (value == null) ? typeof(object) : value.GetType();
                if ((value != null) && !type.IsSerializable)
                {
                    throw new InvalidOperationException(System.Windows.Forms.SR.GetString("NotSerializableType", new object[] { this.name, type.FullName }));
                }
                TypeConverter converter = TypeDescriptor.GetConverter(type);
                bool          flag      = converter.CanConvertTo(typeof(string));
                bool          flag2     = converter.CanConvertFrom(typeof(string));
                try
                {
                    if (flag && flag2)
                    {
                        nodeInfo.ValueData = converter.ConvertToInvariantString(value);
                        nodeInfo.TypeName  = MultitargetUtil.GetAssemblyQualifiedName(type, this.typeNameConverter);
                        return;
                    }
                }
                catch (Exception exception)
                {
                    if (System.Windows.Forms.ClientUtils.IsSecurityOrCriticalException(exception))
                    {
                        throw;
                    }
                }
                bool flag3 = converter.CanConvertTo(typeof(byte[]));
                bool flag4 = converter.CanConvertFrom(typeof(byte[]));
                if (!flag3 || !flag4)
                {
                    if (value == null)
                    {
                        nodeInfo.ValueData = string.Empty;
                        nodeInfo.TypeName  = MultitargetUtil.GetAssemblyQualifiedName(typeof(ResXNullRef), this.typeNameConverter);
                    }
                    else
                    {
                        if (this.binaryFormatter == null)
                        {
                            this.binaryFormatter = new BinaryFormatter();
                        }
                        MemoryStream serializationStream = new MemoryStream();
                        if (this.binaryFormatter == null)
                        {
                            this.binaryFormatter = new BinaryFormatter();
                        }
                        this.binaryFormatter.Serialize(serializationStream, value);
                        string str2 = ToBase64WrappedString(serializationStream.ToArray());
                        nodeInfo.ValueData = str2;
                        nodeInfo.MimeType  = ResXResourceWriter.DefaultSerializedObjectMimeType;
                    }
                }
                else
                {
                    byte[] data = (byte[])converter.ConvertTo(value, typeof(byte[]));
                    string str  = ToBase64WrappedString(data);
                    nodeInfo.ValueData = str;
                    nodeInfo.MimeType  = ResXResourceWriter.ByteArraySerializedObjectMimeType;
                    nodeInfo.TypeName  = MultitargetUtil.GetAssemblyQualifiedName(type, this.typeNameConverter);
                }
            }
        }