C# System.Reflection.Assembly动态加载资源文件


需求:需要做甘特图的显示,并且在甘特中加载图片。图片太多,写判断代码太多。用反射吧。

核心代码:

 try
            {
                if (stateColour < 0) return null;
                System.Reflection.Assembly dll = System.Reflection.Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "WeGanttGmTask.dll");
                string _stateName = GetEnumStrByNum(stateColour);
                _stateName = taskName + _stateName; //图片的名称
                ResourceManager resourceManager = new ResourceManager(dll.GetName().Name + ".Properties.Resources",dll );
                object obj = resourceManager.GetObject(_stateName);
                if (obj == null) return new Bitmap(22, 16);
                Image img = ((System.Drawing.Bitmap)(obj));
                return img;
            }
            catch(Exception e)
            {
                throw e;
            }

  以上的代码,就能获取到资源文件。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM