Пример #1
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);
        }
Пример #2
0
 /// <devdoc>
 ///     Adds a resource to the resources. If the resource is a string,
 ///     it will be saved that way, otherwise it will be serialized
 ///     and stored as in binary.
 /// </devdoc>
 private void AddDataRow(string elementName, string name, object value)
 {
     Debug.WriteLineIf(ResValueProviderSwitch.TraceVerbose, "  resx: adding resource " + name);
     if (value is string)
     {
         AddDataRow(elementName, name, (string)value);
     }
     else if (value is byte[])
     {
         AddDataRow(elementName, name, (byte[])value);
     }
     else if (value is ResXFileRef)
     {
         ResXFileRef  fileRef = (ResXFileRef)value;
         ResXDataNode node    = new ResXDataNode(name, fileRef, this.typeNameConverter);
         if (fileRef != null)
         {
             fileRef.MakeFilePathRelative(BasePath);
         }
         DataNodeInfo info = node.GetDataNodeInfo();
         AddDataRow(elementName, info.Name, info.ValueData, info.TypeName, info.MimeType, info.Comment);
     }
     else
     {
         ResXDataNode node = new ResXDataNode(name, value, this.typeNameConverter);
         DataNodeInfo info = node.GetDataNodeInfo();
         AddDataRow(elementName, info.Name, info.ValueData, info.TypeName, info.MimeType, info.Comment);
     }
 }
 private void AddDataRow(string elementName, string name, object value)
 {
     if (value is string)
     {
         this.AddDataRow(elementName, name, (string)value);
     }
     else if (value is byte[])
     {
         this.AddDataRow(elementName, name, (byte[])value);
     }
     else if (value is ResXFileRef)
     {
         ResXFileRef  fileRef = (ResXFileRef)value;
         ResXDataNode node    = new ResXDataNode(name, fileRef, this.typeNameConverter);
         if (fileRef != null)
         {
             fileRef.MakeFilePathRelative(this.BasePath);
         }
         DataNodeInfo dataNodeInfo = node.GetDataNodeInfo();
         this.AddDataRow(elementName, dataNodeInfo.Name, dataNodeInfo.ValueData, dataNodeInfo.TypeName, dataNodeInfo.MimeType, dataNodeInfo.Comment);
     }
     else
     {
         DataNodeInfo info2 = new ResXDataNode(name, value, this.typeNameConverter).GetDataNodeInfo();
         this.AddDataRow(elementName, info2.Name, info2.ValueData, info2.TypeName, info2.MimeType, info2.Comment);
     }
 }
Пример #4
0
        /// <include file='doc\ResXResourceWriter.uex' path='docs/doc[@for="ResXResourceWriter.AddResource3"]/*' />
        /// <devdoc>
        ///     Adds a string resource to the resources.
        /// </devdoc>
        public void AddResource(ResXDataNode node)
        {
            // we're modifying the node as we're adding it to the resxwriter
            // this is BAD, so we clone it. adding it to a writer doesnt change it
            // we're messing with a copy
            ResXDataNode nodeClone = node.DeepClone();

            ResXFileRef fileRef          = nodeClone.FileRef;
            string      modifiedBasePath = BasePath;

            if (!string.IsNullOrEmpty(modifiedBasePath))
            {
                if (!(modifiedBasePath.EndsWith("\\")))
                {
                    modifiedBasePath += "\\";
                }
                if (fileRef != null)
                {
                    fileRef.MakeFilePathRelative(modifiedBasePath);
                }
            }
            DataNodeInfo info = nodeClone.GetDataNodeInfo();

            AddDataRow(DataStr, info.Name, info.ValueData, info.TypeName, info.MimeType, info.Comment);
        }
        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);
        }
Пример #6
0
        /// <devdoc>
        ///     Adds a resource to the resources. If the resource is a string,
        ///     it will be saved that way, otherwise it will be serialized
        ///     and stored as in binary.
        /// </devdoc>
        private void AddDataRow(string elementName, string name, object value)
        {
            Debug.WriteLineIf(ResValueProviderSwitch.TraceVerbose, "  resx: adding resource " + name);
            switch (value)
            {
            case string str:
                AddDataRow(elementName, name, str);
                break;

            case byte[] bytes:
                AddDataRow(elementName, name, bytes);
                break;

            case ResXFileRef fileRef:
            {
                ResXDataNode node = new ResXDataNode(name, fileRef, this.typeNameConverter);
                DataNodeInfo info = node.GetDataNodeInfo();
                AddDataRow(elementName, info.Name, info.ValueData, info.TypeName, info.MimeType, info.Comment);
                break;
            }

            default:
            {
                ResXDataNode node = new ResXDataNode(name, value, this.typeNameConverter);
                DataNodeInfo info = node.GetDataNodeInfo();
                AddDataRow(elementName, info.Name, info.ValueData, info.TypeName, info.MimeType, info.Comment);
                break;
            }
            }
        }
Пример #7
0
        /// <summary>
        ///    Get the value contained in this datanode
        /// </summary>
        void ISerializable.GetObjectData(SerializationInfo si, StreamingContext context)
        {
            DataNodeInfo nodeInfo = GetDataNodeInfo();

            si.AddValue("Name", nodeInfo.Name, typeof(string));
            si.AddValue("Comment", nodeInfo.Comment, typeof(string));
            si.AddValue("TypeName", nodeInfo.TypeName, typeof(string));
            si.AddValue("MimeType", nodeInfo.MimeType, typeof(string));
            si.AddValue("ValueData", nodeInfo.ValueData, typeof(string));
        }
Пример #8
0
        internal Point ReaderPosition; //only used to track position in the reader

        internal DataNodeInfo Clone()
        {
            DataNodeInfo result = new DataNodeInfo();

            result.Name           = this.Name;
            result.Comment        = this.Comment;
            result.TypeName       = this.TypeName;
            result.MimeType       = this.MimeType;
            result.ValueData      = this.ValueData;
            result.ReaderPosition = new Point(this.ReaderPosition.X, this.ReaderPosition.Y);
            return(result);
        }
Пример #9
0
 private ResXDataNode(SerializationInfo info, StreamingContext context)
 {
     nodeInfo = new DataNodeInfo
     {
         Name      = (string)info.GetValue("Name", typeof(string)),
         Comment   = (string)info.GetValue("Comment", typeof(string)),
         TypeName  = (string)info.GetValue("TypeName", typeof(string)),
         MimeType  = (string)info.GetValue("MimeType", typeof(string)),
         ValueData = (string)info.GetValue("ValueData", typeof(string))
     };
     InitializeDataNode(null);
 }
 private ResXDataNode(SerializationInfo info, StreamingContext context)
 {
     DataNodeInfo info2 = new DataNodeInfo {
         Name = (string) info.GetValue("Name", typeof(string)),
         Comment = (string) info.GetValue("Comment", typeof(string)),
         TypeName = (string) info.GetValue("TypeName", typeof(string)),
         MimeType = (string) info.GetValue("MimeType", typeof(string)),
         ValueData = (string) info.GetValue("ValueData", typeof(string))
     };
     this.nodeInfo = info2;
     this.InitializeDataNode(null);
 }
Пример #11
0
        private ResXDataNode(SerializationInfo info, StreamingContext context)
        {
            DataNodeInfo nodeInfo = new DataNodeInfo();

            nodeInfo.Name      = (string)info.GetValue("Name", typeof(string));
            nodeInfo.Comment   = (string)info.GetValue("Comment", typeof(string));
            nodeInfo.TypeName  = (string)info.GetValue("TypeName", typeof(string));
            nodeInfo.MimeType  = (string)info.GetValue("MimeType", typeof(string));
            nodeInfo.ValueData = (string)info.GetValue("ValueData", typeof(string));
            this.nodeInfo      = nodeInfo;
            InitializeDataNode(null);
        }
        public void AddResource(ResXDataNode node)
        {
            ResXDataNode node2    = node.DeepClone();
            ResXFileRef  fileRef  = node2.FileRef;
            string       basePath = this.BasePath;

            if (!string.IsNullOrEmpty(basePath))
            {
                if (!basePath.EndsWith(@"\"))
                {
                    basePath = basePath + @"\";
                }
                if (fileRef != null)
                {
                    fileRef.MakeFilePathRelative(basePath);
                }
            }
            DataNodeInfo dataNodeInfo = node2.GetDataNodeInfo();

            this.AddDataRow("data", dataNodeInfo.Name, dataNodeInfo.ValueData, dataNodeInfo.TypeName, dataNodeInfo.MimeType, dataNodeInfo.Comment);
        }
Пример #13
0
        private void ParseDataNode(XmlTextReader reader, bool isMetaData)
        {
            DataNodeInfo nodeInfo = new DataNodeInfo
            {
                Name = reader[ResXResourceWriter.NameStr]
            };

            string typeName = reader[ResXResourceWriter.TypeStr];

            string       alias        = null;
            AssemblyName assemblyName = null;

            if (!string.IsNullOrEmpty(typeName))
            {
                alias = GetAliasFromTypeName(typeName);
            }

            if (!string.IsNullOrEmpty(alias))
            {
                assemblyName = _aliasResolver.ResolveAlias(alias);
            }

            if (assemblyName != null)
            {
                nodeInfo.TypeName = GetTypeFromTypeName(typeName) + ", " + assemblyName.FullName;
            }
            else
            {
                nodeInfo.TypeName = reader[ResXResourceWriter.TypeStr];
            }

            nodeInfo.MimeType = reader[ResXResourceWriter.MimeTypeStr];

            bool finishedReadingDataNode = false;

            nodeInfo.ReaderPosition = GetPosition(reader);
            while (!finishedReadingDataNode && reader.Read())
            {
                if (reader.NodeType == XmlNodeType.EndElement && (reader.LocalName.Equals(ResXResourceWriter.DataStr) || reader.LocalName.Equals(ResXResourceWriter.MetadataStr)))
                {
                    // we just found </data>, quit or </metadata>
                    finishedReadingDataNode = true;
                }
                else
                {
                    // could be a <value> or a <comment>
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.Name.Equals(ResXResourceWriter.ValueStr))
                        {
                            WhitespaceHandling oldValue = reader.WhitespaceHandling;
                            try
                            {
                                // based on the documentation at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmltextreaderclasswhitespacehandlingtopic.asp
                                // this is ok because:
                                // "Because the XmlTextReader does not have DTD information available to it,
                                // SignificantWhitepsace nodes are only returned within the an xml:space='preserve' scope."
                                // the xml:space would not be present for anything else than string and char (see ResXResourceWriter)
                                // so this would not cause any breaking change while reading data from Everett (we never outputed
                                // xml:space then) or from whidbey that is not specifically either a string or a char.
                                // However please note that manually editing a resx file in Everett and in Whidbey because of the addition
                                // of xml:space=preserve might have different consequences...
                                reader.WhitespaceHandling = WhitespaceHandling.Significant;
                                nodeInfo.ValueData        = reader.ReadString();
                            }
                            finally
                            {
                                reader.WhitespaceHandling = oldValue;
                            }
                        }
                        else if (reader.Name.Equals(ResXResourceWriter.CommentStr))
                        {
                            nodeInfo.Comment = reader.ReadString();
                        }
                    }
                    else
                    {
                        // weird, no <xxxx> tag, just the inside of <data> as text
                        nodeInfo.ValueData = reader.Value.Trim();
                    }
                }
            }

            if (nodeInfo.Name == null)
            {
                throw new ArgumentException(string.Format(SR.InvalidResXResourceNoName, nodeInfo.ValueData));
            }

            ResXDataNode dataNode = new ResXDataNode(nodeInfo, BasePath);

            if (UseResXDataNodes)
            {
                _resData[nodeInfo.Name] = dataNode;
            }
            else
            {
                IDictionary data = (isMetaData ? _resMetadata : _resData);
                if (_assemblyNames == null)
                {
                    data[nodeInfo.Name] = dataNode.GetValue(_typeResolver);
                }
                else
                {
                    data[nodeInfo.Name] = dataNode.GetValue(_assemblyNames);
                }
            }
        }
 internal ResXDataNode(DataNodeInfo nodeInfo, string basePath)
 {
     this.nodeInfo = nodeInfo;
     this.InitializeDataNode(basePath);
 }
 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;
 }
Пример #16
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), this.typeNameConverter);
                } else {
                    // serialize to string inside the nodeInfo
                    FillDataNodeInfoFromObject(nodeInfo, value);
                }
                
            }
            return nodeInfo;
        }
 private void ParseDataNode(XmlTextReader reader, bool isMetaData)
 {
     DataNodeInfo nodeInfo = new DataNodeInfo {
         Name = reader["name"]
     };
     string str = reader["type"];
     string aliasFromTypeName = null;
     AssemblyName name = null;
     if (!string.IsNullOrEmpty(str))
     {
         aliasFromTypeName = this.GetAliasFromTypeName(str);
     }
     if (!string.IsNullOrEmpty(aliasFromTypeName))
     {
         name = this.aliasResolver.ResolveAlias(aliasFromTypeName);
     }
     if (name != null)
     {
         nodeInfo.TypeName = this.GetTypeFromTypeName(str) + ", " + name.FullName;
     }
     else
     {
         nodeInfo.TypeName = reader["type"];
     }
     nodeInfo.MimeType = reader["mimetype"];
     bool flag = false;
     nodeInfo.ReaderPosition = this.GetPosition(reader);
     while (!flag && reader.Read())
     {
         if ((reader.NodeType == XmlNodeType.EndElement) && (reader.LocalName.Equals("data") || reader.LocalName.Equals("metadata")))
         {
             flag = true;
         }
         else
         {
             if (reader.NodeType == XmlNodeType.Element)
             {
                 if (reader.Name.Equals("value"))
                 {
                     WhitespaceHandling whitespaceHandling = reader.WhitespaceHandling;
                     try
                     {
                         reader.WhitespaceHandling = WhitespaceHandling.Significant;
                         nodeInfo.ValueData = reader.ReadString();
                         continue;
                     }
                     finally
                     {
                         reader.WhitespaceHandling = whitespaceHandling;
                     }
                 }
                 if (reader.Name.Equals("comment"))
                 {
                     nodeInfo.Comment = reader.ReadString();
                 }
                 continue;
             }
             nodeInfo.ValueData = reader.Value.Trim();
         }
     }
     if (nodeInfo.Name == null)
     {
         throw new ArgumentException(System.Windows.Forms.SR.GetString("InvalidResXResourceNoName", new object[] { nodeInfo.ValueData }));
     }
     ResXDataNode node = new ResXDataNode(nodeInfo, this.BasePath);
     if (this.UseResXDataNodes)
     {
         this.resData[nodeInfo.Name] = node;
     }
     else
     {
         IDictionary dictionary = isMetaData ? this.resMetadata : this.resData;
         if (this.assemblyNames == null)
         {
             dictionary[nodeInfo.Name] = node.GetValue(this.typeResolver);
         }
         else
         {
             dictionary[nodeInfo.Name] = node.GetValue(this.assemblyNames);
         }
     }
 }
Пример #18
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 void ParseDataNode(XmlTextReader reader, bool isMetaData) {
            DataNodeInfo nodeInfo = new DataNodeInfo();
            
            nodeInfo.Name = reader[ResXResourceWriter.NameStr];
            string typeName = reader[ResXResourceWriter.TypeStr];

            string alias = null;
            AssemblyName assemblyName = null;
            
            if (!string.IsNullOrEmpty(typeName)) {
                alias  = GetAliasFromTypeName(typeName);
            }
            if (!string.IsNullOrEmpty(alias)) {
                assemblyName = aliasResolver.ResolveAlias(alias);
            }
            if (assemblyName != null )
            {
                nodeInfo.TypeName = GetTypeFromTypeName(typeName) + ", " + assemblyName.FullName;
            }
            else {
                nodeInfo.TypeName = reader[ResXResourceWriter.TypeStr];
            }
            
            nodeInfo.MimeType = reader[ResXResourceWriter.MimeTypeStr];

            bool finishedReadingDataNode = false;
            nodeInfo.ReaderPosition = GetPosition(reader);
            while(!finishedReadingDataNode && reader.Read()) {
                if(reader.NodeType == XmlNodeType.EndElement && ( reader.LocalName.Equals(ResXResourceWriter.DataStr) || reader.LocalName.Equals(ResXResourceWriter.MetadataStr) )) {
                    // we just found </data>, quit or </metadata>
                    finishedReadingDataNode = true;
                } else {
                    // could be a <value> or a <comment>
                    if (reader.NodeType == XmlNodeType.Element) {
                        if (reader.Name.Equals(ResXResourceWriter.ValueStr)) {
                            WhitespaceHandling oldValue = reader.WhitespaceHandling;
                            try {
                                // based on the documentation at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmltextreaderclasswhitespacehandlingtopic.asp 
                                // this is ok because:
                                // "Because the XmlTextReader does not have DTD information available to it,
                                // SignificantWhitepsace nodes are only returned within the an xml:space='preserve' scope." 
                                // the xml:space would not be present for anything else than string and char (see ResXResourceWriter)
                                // so this would not cause any breaking change while reading data from Everett (we never outputed
                                // xml:space then) or from whidbey that is not specifically either a string or a char.
                                // However please note that manually editing a resx file in Everett and in Whidbey because of the addition
                                // of xml:space=preserve might have different consequences...
                                reader.WhitespaceHandling = WhitespaceHandling.Significant;
                                nodeInfo.ValueData = reader.ReadString();
                            } finally {
                                reader.WhitespaceHandling = oldValue;
                            }
                        } else if (reader.Name.Equals(ResXResourceWriter.CommentStr)) {
                            nodeInfo.Comment = reader.ReadString();
                        }
                    } else {
                        // weird, no <xxxx> tag, just the inside of <data> as text
                        nodeInfo.ValueData = reader.Value.Trim();
                    }
                }
            }            

            if (nodeInfo.Name==null) {
                throw new ArgumentException(SR.GetString(SR.InvalidResXResourceNoName, nodeInfo.ValueData));
            }

            ResXDataNode dataNode = new ResXDataNode(nodeInfo, BasePath);

            if(UseResXDataNodes) {
                resData[nodeInfo.Name] = dataNode;
            } else {
                IDictionary data = (isMetaData ? resMetadata : resData);
                if(assemblyNames == null) {
                    data[nodeInfo.Name] = dataNode.GetValue(typeResolver);
                } else {
                    data[nodeInfo.Name] = dataNode.GetValue(assemblyNames);
                }
            }
        }
        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;
        }
Пример #21
0
        private void FillDataNodeInfoFromObject(DataNodeInfo nodeInfo, object value) {           
            CultureInfo ci = value as CultureInfo;
            if( ci != null) { // special-case CultureInfo, cannot use CultureInfoConverter for serialization (see DevDiv#578701).
                nodeInfo.ValueData = ci.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 {
                Type valueType = (value == null) ? typeof(object) : value.GetType();
                if (value != null && !valueType.IsSerializable) {
                    throw new InvalidOperationException(SR.GetString(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, this.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[]));
                    string text = ToBase64WrappedString(data);
                    nodeInfo.ValueData = text;
                    nodeInfo.MimeType = ResXResourceWriter.ByteArraySerializedObjectMimeType;
                    nodeInfo.TypeName = MultitargetUtil.GetAssemblyQualifiedName(valueType, this.typeNameConverter);
                    return;
                }

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

                    MemoryStream ms = new MemoryStream();
                    binaryFormatter.Serialize(ms, value);
                    string text = ToBase64WrappedString(ms.ToArray());
                    nodeInfo.ValueData = text;
                    nodeInfo.MimeType = ResXResourceWriter.DefaultSerializedObjectMimeType;
                }
            }
        
        }
Пример #22
0
        internal Point ReaderPosition; //only used to track position in the reader

        internal DataNodeInfo Clone() {
            DataNodeInfo result  = new DataNodeInfo();
            result.Name = this.Name;
            result.Comment = this.Comment;
            result.TypeName = this.TypeName;
            result.MimeType = this.MimeType;
            result.ValueData = this.ValueData;
            result.ReaderPosition = new Point(this.ReaderPosition.X, this.ReaderPosition.Y);
            return result;
        }
Пример #23
0
 private ResXDataNode(SerializationInfo info, StreamingContext context) {
     DataNodeInfo nodeInfo = new DataNodeInfo();
     nodeInfo.Name = (string)info.GetValue("Name", typeof(string));
     nodeInfo.Comment = (string)info.GetValue("Comment", typeof(string));
     nodeInfo.TypeName = (string)info.GetValue("TypeName", typeof(string));
     nodeInfo.MimeType = (string)info.GetValue("MimeType", typeof(string));
     nodeInfo.ValueData = (string)info.GetValue("ValueData", typeof(string));
     this.nodeInfo = nodeInfo;
     InitializeDataNode(null);
 }
Пример #24
0
 internal ResXDataNode(DataNodeInfo nodeInfo, string basePath)
 {
     this.nodeInfo = nodeInfo;
     InitializeDataNode(basePath);
 }
Пример #25
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 ParseDataNode(XmlTextReader reader, bool isMetaData)
        {
            DataNodeInfo nodeInfo = new DataNodeInfo {
                Name = reader["name"]
            };
            string       str = reader["type"];
            string       aliasFromTypeName = null;
            AssemblyName name = null;

            if (!string.IsNullOrEmpty(str))
            {
                aliasFromTypeName = this.GetAliasFromTypeName(str);
            }
            if (!string.IsNullOrEmpty(aliasFromTypeName))
            {
                name = this.aliasResolver.ResolveAlias(aliasFromTypeName);
            }
            if (name != null)
            {
                nodeInfo.TypeName = this.GetTypeFromTypeName(str) + ", " + name.FullName;
            }
            else
            {
                nodeInfo.TypeName = reader["type"];
            }
            nodeInfo.MimeType = reader["mimetype"];
            bool flag = false;

            nodeInfo.ReaderPosition = this.GetPosition(reader);
            while (!flag && reader.Read())
            {
                if ((reader.NodeType == XmlNodeType.EndElement) && (reader.LocalName.Equals("data") || reader.LocalName.Equals("metadata")))
                {
                    flag = true;
                }
                else
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.Name.Equals("value"))
                        {
                            WhitespaceHandling whitespaceHandling = reader.WhitespaceHandling;
                            try
                            {
                                reader.WhitespaceHandling = WhitespaceHandling.Significant;
                                nodeInfo.ValueData        = reader.ReadString();
                                continue;
                            }
                            finally
                            {
                                reader.WhitespaceHandling = whitespaceHandling;
                            }
                        }
                        if (reader.Name.Equals("comment"))
                        {
                            nodeInfo.Comment = reader.ReadString();
                        }
                        continue;
                    }
                    nodeInfo.ValueData = reader.Value.Trim();
                }
            }
            if (nodeInfo.Name == null)
            {
                throw new ArgumentException(System.Windows.Forms.SR.GetString("InvalidResXResourceNoName", new object[] { nodeInfo.ValueData }));
            }
            ResXDataNode node = new ResXDataNode(nodeInfo, this.BasePath);

            if (this.UseResXDataNodes)
            {
                this.resData[nodeInfo.Name] = node;
            }
            else
            {
                IDictionary dictionary = isMetaData ? this.resMetadata : this.resData;
                if (this.assemblyNames == null)
                {
                    dictionary[nodeInfo.Name] = node.GetValue(this.typeResolver);
                }
                else
                {
                    dictionary[nodeInfo.Name] = node.GetValue(this.assemblyNames);
                }
            }
        }
 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);
         }
     }
 }
 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 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);
                }
            }
        }
Пример #30
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 = (dataNodeInfo.TypeName == null || dataNodeInfo.TypeName.Length==0 ? MultitargetUtil.GetAssemblyQualifiedName(typeof(string), this.typeNameConverter) : dataNodeInfo.TypeName);
            
            if (mimeTypeName != null && mimeTypeName.Length > 0) {
                if (String.Equals(mimeTypeName, ResXResourceWriter.BinSerializedObjectMimeType)
                    || String.Equals(mimeTypeName, ResXResourceWriter.Beta2CompatSerializedObjectMimeType)
                    || String.Equals(mimeTypeName, ResXResourceWriter.CompatBinSerializedObjectMimeType)) {
                    string text = dataNodeInfo.ValueData;
                    byte[] serializedData;
                    serializedData = FromBase64WrappedString(text);

                    if (binaryFormatter == null) {
                        binaryFormatter = new BinaryFormatter();
                        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;
                        }
                    }
                }
#if !SYSTEM_WEB // System.Web does not link with the Soap assembly
                else if (String.Equals(mimeTypeName, ResXResourceWriter.SoapSerializedObjectMimeType)
                         || String.Equals(mimeTypeName, ResXResourceWriter.CompatSoapSerializedObjectMimeType)) {
                    string text = dataNodeInfo.ValueData;
                    byte[] serializedData;
                    serializedData = FromBase64WrappedString(text);

                    if (serializedData != null && serializedData.Length > 0) {

                        // Performance : don't inline a new SoapFormatter here.  That will always bring in
                        //               the soap assembly, which we don't want.  Throw this in another
                        //               function so the class doesn't have to get loaded.
                        //
                        IFormatter formatter = CreateSoapFormatter();
                        result = formatter.Deserialize(new MemoryStream(serializedData));
                        if (result is ResXNullRef) {
                            result = null;
                        }
                    }
                }
#endif
                else if (String.Equals(mimeTypeName, ResXResourceWriter.ByteArraySerializedObjectMimeType)) {
                    if (typeName != null && typeName.Length > 0) {
                        Type type = ResolveType(typeName, typeResolver);
                        if (type != null) {
                            TypeConverter tc = TypeDescriptor.GetConverter(type);
                            if (tc.CanConvertFrom(typeof(byte[]))) {
                                string text = dataNodeInfo.ValueData;
                                byte[] serializedData;
                                serializedData = FromBase64WrappedString(text);
        
                                if (serializedData != null) {
                                    result = tc.ConvertFrom(serializedData);
                                }
                            }
                        }
                        else {
                            string newMessage = SR.GetString(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 (typeName != null && typeName.Length > 0) {
                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 = SR.GetString(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 = SR.GetString(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;
        }