[英]Read resources from a DLL file
I've two Visual Basic 2008 projects - one is a class library project and another is a Windows Forms project. 我有两个Visual Basic 2008项目 - 一个是类库项目,另一个是Windows Forms项目。 In the class library project, I've defined some strings in the project resources (project properties > Resources tab).
在类库项目中,我在项目资源中定义了一些字符串(项目属性>“资源”选项卡)。
I build that class library project and get the DLL file from the debug folder and added up as a reference in my Windows Forms project. 我构建该类库项目并从调试文件夹中获取DLL文件,并在我的Windows窗体项目中添加为参考。
How do I read those strings from the referenced DLL file? 如何从引用的DLL文件中读取这些字符串?
While you can dynamically load the DLL as ho suggests, it's fine to use a reference as you have done. 虽然您可以像h建议的那样动态加载DLL,但是可以像使用的那样使用引用。 In fact I would recommend using a reference unless you had a particular requirement to dynamically load the resource assembly.
事实上,我建议使用引用,除非您有特殊要求动态加载资源程序集。
As to accessing the resources there are a few things you need to do. 至于访问资源,您需要做一些事情。
In the resource assembly you will need to ensure the resources are public. 在资源程序集中,您需要确保资源是公共的。 By default resources are set to internal which means you will not see the resources in the winforms app.
默认情况下,资源设置为internal,这意味着您将无法在winforms应用程序中看到资源。 Double click on Properties\\Resources.resx to open the resources view.
双击Properties \\ Resources.resx以打开资源视图。 In the top toolbar you will see a label "Access Modifier" next to a combo box drop down.
在顶部工具栏中,您将看到组合框下拉旁边的标签“访问修改器”。 Change the selection to public.
将选择更改为公开。
You will need to reference the assembly from the forms app. 您需要从表单应用程序引用程序集。 You have stated you have already done this.
你已经声明你已经这样做了。 Just a note that a better way to do this is to create a solution that contains both projects.
请注意,更好的方法是创建一个包含两个项目的解决方案。 Then in the forms app choose add reference.
然后在表单应用程序中选择添加引用。 Click on the Projects tab up the top.
单击顶部的“项目”选项卡。 Double click on the resource DLL project name.
双击资源DLL项目名称。 This works better than referencing the debug DLL directly since it means if you change between a release build and debug build in your forms app, it will automatically build a matching release/debug version of your resource assembly.
这比直接引用调试DLL更好,因为这意味着如果您在表单应用程序中的发布版本和调试版本之间进行更改,它将自动构建资源程序集的匹配版本/调试版本。
Once you have added the reference you can simply reference the type out of the resources DLL, eg 添加引用后,您可以简单地引用资源DLL中的类型,例如
ResourceDLLNamespace.Properties.Resource.ResourceName ResourceDLLNamespace.Properties.Resource.ResourceName
Just a note, you need to be aware of type name clashes if you are using the same namespace for your forms app and resource DLL. 只需注意,如果您对表单应用程序和资源DLL使用相同的命名空间,则需要注意类型名称冲突。 In this situation both your forms app will have access to it's own Properties.Resources class as well as that of the resource DLL.
在这种情况下,您的表单应用程序都可以访问它自己的Properties.Resources类以及资源DLL的类。 You can do two things to avoid this:
您可以做两件事来避免这种情况:
-Donovan -Donovan
I think you just use System.Reflection.Assembly.Load
to load the other assembly then use the constructor of System.Resources.ResourceManager
that takes an assembly. 我想你只需使用
System.Reflection.Assembly.Load
加载另一个程序集,然后使用带有程序集的System.Resources.ResourceManager
的构造函数。
Note, I don't think it needs to a reference for this to work. 请注意,我认为无需参考此工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.