You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为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);
        }
    }
}

效果动图

组件运行效果动图

资源文件截图

Designer.cs属性截图
testimage.resx属性截图

技术解决方案

  • 检查资源文件配置:确保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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 04:02:35