c# – 如何使XML文件成为vNext(ASP.NET 5)类库中的嵌入式资源?

前端之家收集整理的这篇文章主要介绍了c# – 如何使XML文件成为vNext(ASP.NET 5)类库中的嵌入式资源?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个MVC 6(vNext / ASP.NET 5)项目,一个类库用于DAL(数据访问层).现在我得到一个例外,因为NHibernate找不到我想要坚持的实体的映射文件.我已经看到严格的说明,将这个 XML映射文件标记为嵌入式资源,而不是复制到输出,但是在我为这个文件打开的三个属性页中没有一个,是否有任何规定.

我只想转向基于代码的流畅的映射,但这个问题并不是我的一个NHibernate映射文件所独有的.通过在解决方案资源管理器中右键单击的项目项目的旧属性页就可以了.我希望如果这样一个嵌入式资源的东西依然存在,那么就像project.json那样,我们必须指定这个.

解决方法

UPDATE

我以前的答案不再有效(自RC2起),资源现在被标记为已弃用. (谢谢@Yossarian)

现在正确的方法是使用buildOptions / embed:

  1. ...
  2. "buildOptions": {
  3. "emitEntryPoint": true,"embed": [ "9NLiZmx.png" ]
  4. },...

您必须使用project.json中的部分资源,就像这样

  1. {
  2. "compile": "*.cs","resource": [
  3. "mapping.xml"
  4. ]
  5. }

By default all code files in a directory containing a project.json are included in the project. You can control this with the include/exclude sections of the project.json.

Most sections of the project.json file that deal with files allow glob patterns,which are often called wildcards.

包含/排除属性列表

  1. name default value
  2. ===============================================
  3. compile
  4. compileExclude
  5. content **/*
  6. contentExclude
  7. preprocess compiler/preprocess/**/*.cs
  8. preprocessExclude
  9. resource compiler/preprocess/resources/**/*
  10. resourceExclude
  11. shared compiler/shared/**/*.cs
  12. sharedExclude
  13. publishExclude bin/**;obj/**;**/.*/**
  14. exclude

更多信息:http://docs.asp.net/en/latest/dnx/projects.html#including-excluding-files

您可以看到以下示例:

Program.cs中

  1. using System;
  2. using System.Reflection;
  3.  
  4. namespace ConsoleApp1
  5. {
  6. public class Program
  7. {
  8. public static void Main(string[] args)
  9. {
  10. var assemblyName = new AssemblyName("ConsoleApp1");
  11. var resources = string.Join(Environment.NewLine,Assembly.Load(assemblyName).GetManifestResourceNames());
  12. Console.WriteLine("List of Manifest Resource Names");
  13. Console.WriteLine("======================");
  14. Console.WriteLine(resources);
  15. }
  16. }
  17. }

project.json

  1. {
  2. "version": "1.0.0-*","description": "ConsoleApp1 Console Application","authors": [ "Alberto Monteiro" ],"tags": [ "" ],"projectUrl": "","licenseUrl": "","compilationOptions": {
  3. "emitEntryPoint": true
  4. },"resource": "9NLiZmx.png","dependencies": {
  5. },"commands": {
  6. "ConsoleApp1": "ConsoleApp1"
  7. },"frameworks": {
  8. "dnx451": { },"dnxcore50": {
  9. "dependencies": {
  10. "Microsoft.CSharp": "4.0.1-beta-23516","System.Collections": "4.0.11-beta-23516","System.Console": "4.0.0-beta-23516","System.Linq": "4.0.1-beta-23516","System.Threading": "4.0.11-beta-23516","System.IO": "4.0.11-beta-23516","System.IO.FileSystem": "4.0.1-beta-23225","System.Reflection": "4.1.0-beta-23516"
  11. }
  12. }
  13. }
  14. }

产量

  1. List of Manifest Resource Names
  2. ======================
  3. ConsoleApp1.9NLiZmx.png

猜你在找的C#相关文章