Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Reflection;
  10. using System.IO;
  11. using PluginInterface;
  12.  
  13. namespace OOP_UI
  14. {
  15. public partial class FMain : Form
  16. {
  17.  
  18. //список создаваемых предприятий
  19. public List<Enterprises> EnterprisesList = new List<Enterprises>()
  20. {
  21. new Fishing() { Name = "Артель рыболов «Гетеборг»", AmountOfWorkers = 50, Locations = "г. Брянск",
  22. OnLand = false, OnWater = true, InOcean = false,
  23. InSea = false, InRiver = true, Fish = 10000
  24. },
  25.  
  26. new MiningEnterprise() { Name = "ТОО \"Оркен\"", AmountOfWorkers = 100, Locations = "Степногорск",
  27. OnLand = true, OnWater = false, Mine = false,
  28. Quarry = true, DurationOfMining = 1365, LevelOfDanger = 2,
  29. },
  30.  
  31. new HydroPowerPlant() { Name = "Саяно-Шушенская ГЭС", AmountOfWorkers = 70, Locations = "Саяногорск",
  32. OnLand = false, OnWater = true, PowerOfStation = 6400,
  33. PowerOfWaterPressure = 194, TypeOfStation = HydroPowerPlant.Stations.Accumulating
  34. },
  35.  
  36. new ThermalPowerPlant() { Name = "Сургутская ГРЭС-1", AmountOfWorkers = 65, Locations = "г. Сургут",
  37. LocalRawMaterial = false, DangerRawMaterial = true,
  38. TypeOfFuel = ThermalPowerPlant.Fuels.NatureGas
  39. },
  40.  
  41. new BakeryEnterprise() { Name = "ООО Уком", AmountOfWorkers = 53, Locations = "г. Новосибирск",
  42. ProductsWithFlour = true, ProductsWithMeat = true,
  43. ProductsWithMilk = false, ProductsWithSugar = true,
  44. LocalRawMaterial = false, DangerRawMaterial = false,
  45. AmountOfFlour = 120, Eggs = 300000, Milk = 700, Sail = 200,
  46. Sugar = 300, TypeOfFlour = BakeryEnterprise.Flours.Wheat, Water = 2
  47. },
  48.  
  49. new MeatProcessingPlant() { Name = "Перерабатывающий завод \"Витебск Мясокомбината\"", AmountOfWorkers = 15,
  50. Locations = "г. Витебск", LocalRawMaterial = true, DangerRawMaterial = false,
  51. ProductsWithFlour = false, ProductsWithMeat = true,
  52. ProductsWithMilk = false, ProductsWithSugar = false,
  53. CannedFood = true, Sausages = true, Semis = true,
  54. LocalMeat = true, NeededMeat = 1000
  55. },
  56.  
  57. new MeatCombine() { Name = "\"ОАО Витебск Мясокомбинат\"", AmountOfWorkers = 53, Locations = "г. Витебск",
  58. LocalRawMaterial = false, DangerRawMaterial = false,
  59. ProductsWithFlour = false, ProductsWithMeat = true,
  60. ProductsWithMilk = false, ProductsWithSugar = false,
  61. AmountOfCattle = 400, AmountOfFreshMeat = 400, KeepingMeat = 700
  62. },
  63.  
  64. new MilkEnterprise() { Name = "\"ОАО Витебск Молоко\"", AmountOfWorkers = 34, Locations = "г. Витебск",
  65. LocalRawMaterial = false, DangerRawMaterial = false,
  66. ProductsWithFlour = false, ProductsWithMeat = false,
  67. ProductsWithMilk = true, ProductsWithSugar = true,
  68. }
  69.  
  70. };
  71.  
  72. //список типов каждого предприятия
  73. public List<Type> TypesList = new List<Type>()
  74. {
  75. typeof(Fishing),
  76. typeof(MiningEnterprise),
  77. typeof(HydroPowerPlant),
  78. typeof(ThermalPowerPlant),
  79. typeof(BakeryEnterprise),
  80. typeof(MeatProcessingPlant),
  81. typeof(MeatCombine),
  82. typeof(MilkEnterprise)
  83. };
  84.  
  85. public List<IPlugin> PluginsList = new List<IPlugin>();
  86.  
  87. public int SelectedIndex;
  88. public Form FEdit;
  89.  
  90. public FMain()
  91. {
  92. InitializeComponent();
  93. }
  94.  
  95. //загрузка формы
  96. private void FMain_Load(object sender, EventArgs e)
  97. {
  98. LVMain.MultiSelect = false;
  99. SelectedIndex = -1;
  100.  
  101. foreach (var type in TypesList)
  102. CBTypes.Items.Add(type.ToString().Substring(7));
  103.  
  104. CBTypes.SelectedIndex = 0;
  105. CBTypes.DropDownStyle = ComboBoxStyle.DropDownList;
  106.  
  107. LoadPlugins();
  108.  
  109. Redraw(LVMain, EnterprisesList);
  110. }
  111.  
  112. //перерисовка списка
  113. public void Redraw(ListView listView, List<Enterprises> items)
  114. {
  115. listView.Clear();
  116.  
  117. for (int i = 0; i < items.Count; i++)
  118. {
  119. var LVItem = new ListViewItem();
  120. Type itemType = items[i].GetType();
  121.  
  122. object Name;
  123.  
  124. try
  125. {
  126. var nameField = items[i].GetType().GetField("Name");
  127. Name = nameField.GetValue(items[i]);
  128. }
  129. catch
  130. {
  131. Name = "";
  132. }
  133.  
  134. LVItem.Text = itemType.Name + " " + Name;
  135. listView.Items.Add(LVItem);
  136. }
  137. }
  138.  
  139. //создание нового предприятия
  140. private void BCreate_Click(object sender, EventArgs e)
  141. {
  142. Enterprises newObject = (Enterprises) Activator.CreateInstance(TypesList[CBTypes.SelectedIndex]);
  143. EnterprisesList.Add(newObject);
  144.  
  145. Form EForm = new ItemForm(newObject, EnterprisesList);
  146. EForm.StartPosition = FormStartPosition.CenterScreen;
  147. EForm.ShowDialog();
  148. EForm.Dispose();
  149.  
  150. Redraw(LVMain, EnterprisesList);
  151. }
  152.  
  153. //редактирование выбранного предприятия
  154. private void BEdit_Click(object sender, EventArgs e)
  155. {
  156.  
  157. //получаем индекc выделенного пункта
  158. int itemNum;
  159. if (LVMain.SelectedIndices.Count != 0)
  160. itemNum = LVMain.SelectedIndices[0];
  161. else
  162. return;
  163.  
  164. //Создаем форму редактирования обьекта
  165. Form EForm = new ItemForm(EnterprisesList[itemNum], EnterprisesList);
  166. EForm.StartPosition = FormStartPosition.CenterScreen;
  167. EForm.ShowDialog();
  168. EForm.Dispose();
  169.  
  170. Redraw(LVMain, EnterprisesList);
  171.  
  172. }
  173.  
  174. private void Delete_Action(Enterprises DelObject, List<Enterprises> ObjectList)
  175. {
  176. //список объектов которые могут использовать удаляемый обьект
  177. var ownerList = ObjectList.Where(item => (item.GetType().GetFields().Where(field => (field.FieldType == DelObject.GetType()))).ToList().Count > 0);
  178. foreach (var owner in ownerList)
  179. {
  180. foreach (var field in owner.GetType().GetFields().Where(field => (field.FieldType == DelObject.GetType())).ToList())
  181. {
  182. if (field.GetValue(owner) != null)
  183. {
  184. if (field.GetValue(owner).Equals(DelObject))
  185. {
  186. field.SetValue(owner, null);
  187. }
  188. }
  189. }
  190. }
  191.  
  192. //Непосредственное удаление обьекта
  193. ObjectList.Remove(DelObject);
  194. }
  195.  
  196. //удаление выбранного предприятия
  197. private void BDelete_Click(object sender, EventArgs e)
  198. {
  199. if ((LVMain.SelectedIndices.Count != 0) && (LVMain.SelectedIndices[0] < EnterprisesList.Count))
  200. {
  201. int itemNum = LVMain.SelectedIndices[0];
  202.  
  203. Delete_Action(EnterprisesList[itemNum], EnterprisesList);
  204. }
  205. Redraw(LVMain, EnterprisesList);
  206. }
  207.  
  208. private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
  209. {
  210. TypeOfFile fTypeOfFile = new TypeOfFile(EnterprisesList, PluginsList, "save");
  211. this.Hide();
  212. fTypeOfFile.ShowDialog();
  213. fTypeOfFile.Dispose();
  214. this.Show();
  215. }
  216.  
  217. private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
  218. {
  219. TypeOfFile fTypeOfFile = new TypeOfFile(EnterprisesList, PluginsList, "load");
  220. this.Hide();
  221. fTypeOfFile.ShowDialog();
  222. EnterprisesList = fTypeOfFile.TheValue;
  223. fTypeOfFile.Dispose();
  224. Redraw(LVMain, EnterprisesList);
  225. this.Show();
  226. }
  227.  
  228. private void LoadPlugins()
  229. {
  230. string pluginPath = Path.Combine(Directory.GetCurrentDirectory(),"Plugins");
  231.  
  232. DirectoryInfo pluginDirectory = new DirectoryInfo(pluginPath);
  233. if (!pluginDirectory.Exists)
  234. pluginDirectory.Create();
  235.  
  236. //Берем из директории все файлы с расширением .dll
  237. var pluginFiles = Directory.GetFiles(pluginPath, "*.dll");
  238. foreach (var file in pluginFiles)
  239. {
  240. if (file != (pluginPath + "\\" + "PluginInterface.dll"))
  241. {
  242. //Загружаем сборку
  243. Assembly asm = Assembly.LoadFrom(file);
  244. //Ищем типы, имплементирующие наш интерфейс IPlugin
  245. var types = asm.GetTypes().
  246. Where(t => t.GetInterfaces().
  247. Where(i => i.FullName == typeof(IPlugin).FullName).Any());
  248.  
  249. //Заполняем экземплярами полученных типов коллекцию плагинов
  250. foreach (var type in types)
  251. {
  252. var plugin = asm.CreateInstance(type.FullName) as IPlugin;
  253. PluginsList.Add(plugin);
  254. }
  255. }
  256. }
  257. }
  258.  
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement