如何为C#反编译库的自定义组件自动配置ComponentResourceManager图像
问题:反编译C#自定义组件的ComponentResourceManager图像无法加载
我正尝试为反编译得到的C#自定义组件自动配置ComponentResourceManager图像。之前开发过同类类库,但存储源码的硬盘损坏,只能通过反编译现有库继续开发,现在遇到资源图像无法被检测到的错误。以下是自定义组件testimage的Designer.cs代码、效果动图及资源文件截图,寻求技术指导。
Designer.cs代码
using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Resources; using System.Windows.Forms; namespace testimagelibrary { [DebuggerStepThrough] [DefaultEvent("Click")] [ProvideProperty("testimage", typeof(Control))] public class testimage : PictureBox { private int _Zoom = 10; private System.Drawing.Image _imgActive = null; private System.Drawing.Image _tmp = null; private IContainer components = null; public System.Drawing.Image ImageActive { get { return this._imgActive; } set { this._imgActive = value; } } public int Zoom { get { return this._Zoom; } set { this._Zoom = value; } } public testimage() { this.InitializeComponent(); } protected override void Dispose(bool disposing) { if ((!disposing ? false : this.components != null)) { this.components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(testimage)); ((ISupportInitialize)this).BeginInit(); base.SuspendLayout(); base.Image = (System.Drawing.Image)componentResourceManager.GetObject("this.Image"); base.Size = new System.Drawing.Size(71, 71); base.SizeMode = PictureBoxSizeMode.Zoom; ((ISupportInitialize)this).EndInit(); base.ResumeLayout(false); } } }
效果动图

资源文件截图


技术解决方案
- 检查资源文件配置:确保
testimage.resx的生成操作设置为嵌入的资源,且与testimage类处于同一命名空间,同时确认资源文件中存在键为this.Image的图像资源(名称区分大小写)。 - 修正资源加载逻辑:反编译代码可能存在资源路径偏差,可显式指定程序集加载资源:
ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(testimage).Assembly, typeof(testimage).FullName); - 验证项目文件配置:打开.csproj文件,确认资源项关联正确:
<EmbeddedResource Include="testimage.resx"> <DependentUpon>testimage.cs</DependentUpon> </EmbeddedResource> - 调试资源状态:添加调试代码验证资源是否存在:
if(componentResourceManager.GetObject("this.Image") == null) { Debug.WriteLine("资源this.Image未找到"); var assembly = typeof(testimage).Assembly; using(var stream = assembly.GetManifestResourceStream("testimagelibrary.testimage.resx")) { Debug.WriteLine(stream != null ? "资源文件存在" : "资源文件未嵌入程序集"); } } - 重建资源文件:若反编译的resx文件损坏,可删除后重新创建与
testimage类绑定的资源文件,手动添加对应图像资源。
内容的提问来源于stack exchange,提问作者dlaksmi
相关产品推荐
相关产品推荐

