6

Is it possible to retain the logical layout of the project file?

If I have 2 embedded resources laid out like the following

Embedded Resources

When I call Assembly.GetManifestResourceNames(), the two of them get named:

  • MyLibrary._MyItems.SubItems.SubItem1.xml
  • MyLibrary._MyItems.SubItems.SubItem2.xml

However, this gives me no real insight into how they're logically ordered in my project. For all I know, they could both be in the same directory.

I worry that this may not be possible, because if I name the files like so, my app will not even compile:

Embedded Resources

I want to be able to distinguish between

  • MyLibrary._MyItems\SubItems\SubItem1.xml
  • and
  • MyLibrary._MyItems\SubItems.SubItem2.xml

Similar question, but less detail than I am looking for

Exact duplicate, no traction

4

1 回答 1

0

一个简单的解决方案是提出一个约定来指示文件名的开始位置。然后,您可以“安全地”假设名称的其余部分表示文件夹结构。

但是,由于这些是嵌入式资源,您可以创建在编译之前运行的 T4 脚本(或一些类似的代码),以检查文件夹结构并构建详细说明该结构的类。

这是生成的类的外观示例。

public static class MyResources
{
    private readonly Dictionary<String, String> ResourceNameToDirPathMappingSetSource = new Dictionary<String, String>();

    static MyResources()
    {
        ResourceNameToPathMappingSetSource.Add("MyLibrary._MyItems.SubItems.SubItem1.xml", @"MyLibrary._MyItems\SubItems");
        ResourceNameToPathMappingSetSource.Add("MyLibrary._MyItems.SubItems.SubItem2.xml", @"MyLibrary._MyItems");
    }

    public static IReadOnlyDictionary<String, String> ResourceNameToDirPathMappingSet
    {
        get
        {
            return ResourceNameToDirPathMappingSetSource;
        }
    }
}

我的假设是您想要获取资源名称并确定它最初所在的文件夹路径。字典当然可以包含您需要映射到的任何值(阅读:自定义类)。

于 2013-01-24T20:49:04.347 回答