When using the ResXResourceReader how can tell if the resource is an embedded file or if it is an embedded string

怎甘沉沦 提交于 2019-12-21 12:20:38

问题


I have a separate application (that is for the purpose of spell checking my .resx files) that runs as a pre-build event. However, if the .resx file contains a text file (xml for example) my application will load the file and attempt to spell check it. This is not really what I want it to do. Is there a way to tell from the ResXResourceReader if the resource loaded is actually a file?

Code sample looks like this:

                ResXResourceReader reader = new ResXResourceReader(filename);
                ResourceSet resourceset = new ResourceSet(reader);

                Dictionary<DictionaryEntry, object> newvalues = new Dictionary<DictionaryEntry, object>();

                foreach (DictionaryEntry entry in resourceset)
                {
                    //Figure out in this 'if' if it is an embedded file and should be ignored.
                    if (entry.Key.ToString().StartsWith(">>") || !(entry.Value is string) || string.Compare((string)entry.Value, "---") == 0)
                        continue;
                 }

回答1:


Yes. Setting UseResXDataNodes on ResXResourceReader will cause the dictionary values to be a ResXDataNode instead of the actual value, which you can use to determine if it is a file or not. Something like this:

var rsxr = new ResXResourceReader("Test.resx");
rsxr.UseResXDataNodes = true;
foreach (DictionaryEntry de in rsxr)
{
    var node = (ResXDataNode)de.Value;
    //FileRef is null if it is not a file reference.
    if (node.FileRef == null)
    {
        //Spell check your value.
        var value = node.GetValue((ITypeResolutionService) null);
    }
}


来源:https://stackoverflow.com/questions/8158544/when-using-the-resxresourcereader-how-can-tell-if-the-resource-is-an-embedded-fi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!