Ik ben niet echt een specialist in C#. Ik weet de beginselen, normaal programmeer ik alleen PHP maar ik wilde iets anders proberen. Ik probeer momenteel een systeem te ontwikkelen waarin plugins de GUI en functionaliteiten van de applicatie maken. Ik heb een soort draft gemaakt maar ik heb totaal geen idee of dit de juiste methode is en de juiste weg.
Tot nu toe heb ik deze code; hopelijk kan iemand mij in de juiste richting sturen. Er komen veel foutmeldingen in voor en ik denk dat iPlugin totaal niet iets is waar ik naar op zoek ben. Google vertelt mij weinig en vertelt mij alleen veel over console apps ipv. gui apps.
Structuur:
Tot nu toe heb ik deze code; hopelijk kan iemand mij in de juiste richting sturen. Er komen veel foutmeldingen in voor en ik denk dat iPlugin totaal niet iets is waar ik naar op zoek ben. Google vertelt mij weinig en vertelt mij alleen veel over console apps ipv. gui apps.
Structuur:
- App - Nu alleen lege form en lege program.cs (hier wordt de gui en de plugins geladen)
- App.Core (de plugin core en voor language strings etc.)
- Example Plugin
C#: App.Core/Classes/IPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
| namespace App.Core { public interface IPlugin { string Name { get; } string Version { get; } bool OnLoad(); void OnConfigure(); object DataExchange(object objData); } } |
C#: App.Core/Classes/PluginCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
| using System; using System.Collections; namespace App.Core { public class PluginCollection : CollectionBase { Collection iPluginCollection; // The type or namespace name 'Collection' could not be found (are you missing a using directive or an assembly reference?) private IPlugin Plugin; public virtual void Add(IPlugin Plugin) { innerlist.Add(Plugin); // The name 'innerlist' does not exist in the current context } public virtual void AddRange(IPlugin[] Plugin) { innerlist.AddRange(Plugin); // The name 'innerlist' does not exist in the current context } public IPlugin Item(Int32 Index) { return innerlist.Item[Index]; // The name 'innerlist' does not exist in the current context } public IPlugin Item(string Name) // Shit, this is not working { IPlugin plug; foreach (plug in innerlist) { if ((plug.Name.ToLower().CompareTo(Name.ToLower()) == 0)) { return plug; } } } public void Remove(IPlugin Plugin) { innerlist.Remove(Plugin); // The name 'innerlist' does not exist in the current context } public PluginCollection() { iPluginCollection = new Collection(); // The type or namespace name 'Collection' could not be found (are you missing a using directive or an assembly reference?) } protected override void Finalize() // Intend to declare a destructor? Is this even needed? { iPluginCollection = null; base.Finalize(); // IDE Suggests not to call this due to garbage because it gets called by the destructor? I have pain in my head } } } |
C#: App.Core/Classes/PluginLocator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
| using System; using System.Reflection; namespace App.Core { [Serializable()] public class PluginLocator { public PluginCollection FindPlugins(string path) { if (!System.IO.Directory.Exists(path)) { return default(PluginCollection); } PluginCollection results = new PluginCollection(); string[] files = System.IO.Directory.GetFiles(path, "*.*"); Type iface = default(Type); Type t = default(Type); string file = ""; foreach (string _file in files) { file = _file; try { Assembly asm = Assembly.LoadFrom(file); foreach (Type loop_t in asm.GetTypes()) { t = loop_t; foreach (Type loop_v_iface in t.GetInterfaces()) { iface = loop_v_iface; if (typeof(IPlugin) == iface) { IPlugin plug = (IPlugin)(Activator.CreateInstance(t)); if (plug.OnLoad()) { results.Add(plug); } } } } } catch (Exception) { } } return results; } } } |
C#: ExamplePlugin/ExamplePlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| using App.Core; namespace App.Plugins { public class ExamplePlugin : IPlugin { public string Name { get { return "Example Plugin"; } } public string Version { get { return "0.0.1"; } } public bool OnLoad() { return true; } public void OnConfigure() { // } public object DataExchange(object objData) { return null; } } } |
.Gertjan.: Ik ben een zelfstandige alcoholist, dus ik bepaal zelf wel wanneer ik aan het bier ga!