需求:須要作甘特圖的顯示,而且在甘特中加載圖片。圖片太多,寫判斷代碼太多。用反射吧。blog
核心代碼:圖片
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; }
以上的代碼,就能獲取到資源文件。資源