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; }
以上的代码,就能获取到资源文件。