在 C# 中使用反射来尝试访问 My.Resources

发布于 2024-12-14 16:37:16 字数 1531 浏览 6 评论 0原文

我继承了一个用 C# 和 VB.Net 混合编写的大型项目,

该项目涉及许多单独的程序集。

有数百(或数千)个资源(png 文件)已使用 VB My.Resources 功能插入到项目中,我想从不同程序集中的某些 C# 代码访问这些资源。

Microsoft 有一篇关于此问题的知识库文章。它包含一些示例代码,但我无法让它工作(并且我不能完全遵循代码)。

// Gets a reference to the same assembly that 
// contains the type that is creating the ResourceManager.
System.Reflection.Assembly myAssembly;
myAssembly = this.GetType().Assembly;

// Gets a reference to a different assembly.
System.Reflection.Assembly myOtherAssembly;
myOtherAssembly = System.Reflection.Assembly.Load("ResourceAssembly");

// Creates the ResourceManager.
System.Resources.ResourceManager myManager = new 
    System.Resources.ResourceManager("ResourceNamespace.myResources", myAssembly);

// Retrieves String and Image resources.
System.String myString;
System.Drawing.Image myImage;
myString = myManager.GetString("StringResource");
myImage = (System.Drawing.Image)myManager.GetObject("ImageResource");

我不明白为什么代码创建 myAssemblymyOtherAssembly 并且似乎从未使用第二个对象?

加载第二个程序集的操作是否

myOtherAssembly = System.Reflection.Assembly.Load("ResourceAssembly");

意味着我在创建 ResourceManager 时不需要进一步引用它?

System.Resources.ResourceManager myManager = new 
    System.Resources.ResourceManager("ResourceNamespace.myResources", myAssembly);

任何关于我如何访问这些资源(有或没有反射)的想法将不胜感激!

I have inherited a large project written in a mixture of C# and VB.Net

The project involves many separate assemblies.

There are hundreds (or thousands) of resources (png files) that have been inserted into the project using the VB My.Resources functionality that I would like to access from some C# code, in a different assembly.

Microsoft has a KB article about this. It includes some sample code, but I can't get it to work (and I can't quite follow the code).

// Gets a reference to the same assembly that 
// contains the type that is creating the ResourceManager.
System.Reflection.Assembly myAssembly;
myAssembly = this.GetType().Assembly;

// Gets a reference to a different assembly.
System.Reflection.Assembly myOtherAssembly;
myOtherAssembly = System.Reflection.Assembly.Load("ResourceAssembly");

// Creates the ResourceManager.
System.Resources.ResourceManager myManager = new 
    System.Resources.ResourceManager("ResourceNamespace.myResources", myAssembly);

// Retrieves String and Image resources.
System.String myString;
System.Drawing.Image myImage;
myString = myManager.GetString("StringResource");
myImage = (System.Drawing.Image)myManager.GetObject("ImageResource");

I can't follow why the code creates myAssembly and myOtherAssembly and never seems to use the second object?

Does the action of loading the second assembly...

myOtherAssembly = System.Reflection.Assembly.Load("ResourceAssembly");

...mean I don't need to reference it further down when I create a ResourceManager ?

System.Resources.ResourceManager myManager = new 
    System.Resources.ResourceManager("ResourceNamespace.myResources", myAssembly);

Any idea how I can access these resources (with or without reflection) would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2024-12-21 16:37:22

尝试此示例

http://msdn.microsoft.com/en-us/library/ yfsz7ac5.aspx

当您说加载 myOtherAssembly 意味着您以后不必引用它时,您是对的。不过,您实际上并不需要声明局部变量来执行此操作。

我不知道为什么它不起作用。您确定资源命名空间正确吗?如果在ResourceManager实例化后添加断点,在调试器中能看到里面有资源吗?

Try this sample

http://msdn.microsoft.com/en-us/library/yfsz7ac5.aspx

You're right when you say loading myOtherAssembly means you don't have to reference it later. You don't really need to declare a local variable to do it though.

I'm not sure why it wouldn't work. Are you sure the resource namespace is correct? If you add a breakpoint after the ResourceManager is instantiated, can you see any resources in it in the debugger?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文