예제 #1
0
        /// <summary>
        ///     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.
        /// </summary>
        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);
            }
        }
예제 #2
0
        /// <summary>
        ///     Adds a string resource to the resources.
        /// </summary>
        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);
        }
예제 #3
0
        /// <summary>
        ///    Get the value contained in this datanode
        /// </summary>
        public object GetValue(ITypeResolutionService typeResolver)
        {
            if (value != null)
            {
                return(value);
            }

            object result = null;

            if (FileRefFullPath != null)
            {
                Type objectType = ResolveType(FileRefType, typeResolver);
                if (objectType != null)
                {
                    // we have the FQN for this type
                    if (FileRefTextEncoding != null)
                    {
                        fileRef = new ResXFileRef(FileRefFullPath, objectType.AssemblyQualifiedName,
                                                  Encoding.GetEncoding(FileRefTextEncoding));
                    }
                    else
                    {
                        fileRef = new ResXFileRef(FileRefFullPath, objectType.AssemblyQualifiedName);
                    }

                    TypeConverter tc = TypeDescriptor.GetConverter(typeof(ResXFileRef));
                    result = tc.ConvertFrom(fileRef.ToString());
                }
                else
                {
                    string            newMessage = string.Format(SR.TypeLoadExceptionShort, FileRefType);
                    TypeLoadException newTle     = new TypeLoadException(newMessage);
                    throw (newTle);
                }
            }
            else if (nodeInfo.ValueData != null)
            {
                // it's embedded, we deserialize it
                result = GenerateObjectFromDataNodeInfo(nodeInfo, typeResolver);
            }
            else
            {
                // schema is wrong and say minOccur for Value is 0,
                // but it's too late to change it...
                // we need to return null here
                return(null);
            }

            return(result);
        }
예제 #4
0
        public ResXDataNode(string name, ResXFileRef fileRef, Func <Type, string> typeNameConverter)
        {
            if (name == null)
            {
                throw (new ArgumentNullException(nameof(name)));
            }

            if (name.Length == 0)
            {
                throw (new ArgumentException(nameof(name)));
            }

            this.name              = name;
            this.fileRef           = fileRef ?? throw (new ArgumentNullException(nameof(fileRef)));
            this.typeNameConverter = typeNameConverter;
        }
예제 #5
0
 public ResXDataNode(string name, ResXFileRef fileRef)
     : this(name, fileRef, null)
 {
 }