如何从该基础 class 的另一个 VS 项目中的基础 class 加载嵌入式资源
How to load an embedded resource from a base class that is in another VS project from that base class
正在尝试将 XSD 嵌入由多个派生 class 程序集使用的基础 class 程序集中。
Visual Studio 2013年项目A:
namespace A.B.C
{
public abstract class WidgetBase {
protected virtual void LoadSchema() {
var schemas = new XmlSchemaSet();
using (var resourceStream = this.GetType().Assembly.GetManifestResourceStream("A.B.C.Schemas.SchemaA.xsd"))
{
...Load the schema...
}
}
}
}
在 csproj 2 中,我有一个 class 使用该基 class:
Visual Stdio 2013 项目 B
namespace D.E
{
public class WidgetDerived : WidgetBase {
public string DoSomethingWithXml(string xmlFile)
{
var schema = base.LoadSchema();
...do something...
}
}
}
问题是我无法在对象浏览器中找到 XSD 或将资源路径设置为:
var resourceStream = this.GetType().Assembly.GetManifestResourceStream("A.B.C.Schemas.SchemaA.xsd")
var resourceStream = typeof(WidgetBase).Assembly.GetManifestResourceStream("A.B.C.Schemas.SchemaA.xsd")
var resourceStream = typeof(WidgetBase).Assembly.GetManifestResourceStream("D.E.Schemas.SchemaA.xsd")
如果我从相对磁盘路径加载 XSD,代码运行良好。作为嵌入式资源,尽管我不太确定它的去向。我认为它将嵌入基础 class 程序集中,因此对自身可见。
当资源在另一个程序集和同一个程序集中(这里和 https://support.microsoft.com/en-us/kb/319292 以及许多其他程序集)时,我看到了使用 GetManifestResourceStream 的其他答案,但在加载基础 class 时却没有在从派生 class 调用后它自己的程序集。理论上它应该与从自己加载时完全相同,但它不是那样工作的。
粗鲁。我首先尝试的东西,但有一个错字:
var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("A.B.C.Schemas.FuzzyTestPlan.xsd")
因此,仅供参考,它确实使用代码的相关程序集的名称空间和路径。只需确保您正确输入即可。 :s
正在尝试将 XSD 嵌入由多个派生 class 程序集使用的基础 class 程序集中。
Visual Studio 2013年项目A:
namespace A.B.C
{
public abstract class WidgetBase {
protected virtual void LoadSchema() {
var schemas = new XmlSchemaSet();
using (var resourceStream = this.GetType().Assembly.GetManifestResourceStream("A.B.C.Schemas.SchemaA.xsd"))
{
...Load the schema...
}
}
}
}
在 csproj 2 中,我有一个 class 使用该基 class:
Visual Stdio 2013 项目 B
namespace D.E
{
public class WidgetDerived : WidgetBase {
public string DoSomethingWithXml(string xmlFile)
{
var schema = base.LoadSchema();
...do something...
}
}
}
问题是我无法在对象浏览器中找到 XSD 或将资源路径设置为:
var resourceStream = this.GetType().Assembly.GetManifestResourceStream("A.B.C.Schemas.SchemaA.xsd")
var resourceStream = typeof(WidgetBase).Assembly.GetManifestResourceStream("A.B.C.Schemas.SchemaA.xsd")
var resourceStream = typeof(WidgetBase).Assembly.GetManifestResourceStream("D.E.Schemas.SchemaA.xsd")
如果我从相对磁盘路径加载 XSD,代码运行良好。作为嵌入式资源,尽管我不太确定它的去向。我认为它将嵌入基础 class 程序集中,因此对自身可见。
当资源在另一个程序集和同一个程序集中(这里和 https://support.microsoft.com/en-us/kb/319292 以及许多其他程序集)时,我看到了使用 GetManifestResourceStream 的其他答案,但在加载基础 class 时却没有在从派生 class 调用后它自己的程序集。理论上它应该与从自己加载时完全相同,但它不是那样工作的。
粗鲁。我首先尝试的东西,但有一个错字:
var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("A.B.C.Schemas.FuzzyTestPlan.xsd")
因此,仅供参考,它确实使用代码的相关程序集的名称空间和路径。只需确保您正确输入即可。 :s