How to load my test data in C#?

冷暖自知 提交于 2019-12-21 02:57:35

问题


I am struggling to find an easy way to load my test data in C#.

In Java, I load a resource using the following code:

...

public static InputStream loadResource(String resource) throws LoadException {
    InputStream is = TestUtils.class.getResourceAsStream(resource);

    if (is == null) {
        throw new LoadException("Error loading '" + resource + "'");
    }

    return is;
}

...

public static void main(String[] args) {
    InputStream is = TestUtils.loadResource("/resourcelocation");
}

I tried to use C# resource file, but I found awkward to load and manipulate it. Is there a simpler way to load resources in C#?


回答1:


Yes - use Assembly.GetManifestResourceStream, e.g.

typeof(TestClass).Assembly
                 .GetManifestResourceStream("test.namespace.Filename.txt")

Just make sure the files are tagged as "Embedded Resource" in the properties, so they get built into the assembly correctly.



来源:https://stackoverflow.com/questions/1069436/how-to-load-my-test-data-in-c

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