Generic parameter to string
-
1 private void AddPlugin(Assembly pluginAssembly) { 2 3 //Loop through all the Types found in the assembly 4 foreach (Type pluginType in pluginAssembly.GetTypes()) { 5 //Only look at public and non-abstract types 6 if (pluginType.IsPublic && !pluginType.IsAbstract) { 7 Type typeInterface = null; 8 try { 9 //Gets a type object of the interface we need the plugins to match 10 typeInterface = pluginType.GetInterface("IPlugin", true); 11 } 12 catch (Exception) { } 13 14 //Make sure the interface we want to use actually exists 15 if (typeInterface1 != null) { 16 //Create a new available plugin instance 17 T newPlugin = 18 (T)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); 19 20 availablePlugins.Add(newPlugin); 21 } 22 } 23 } 24 } Here's a method I've got in a generic PluginManager class with parameter T. It adds a plugin of the right type to a list of available plugins. The problem I've got is in line 10. Instead of "IPlugin" (The parent interface of the different interfaces for plugins (IParser, ISolution, etc.), something like "T" should be used. Unfortunatly I couldn't find out how to get the name of the parameter T as a string. ToString and casting doesn't work on the generic parameter. Is there any way of getting the right string ("IParser", "ISolution", etc.) depending on the chosen parameter T?
-
1 private void AddPlugin(Assembly pluginAssembly) { 2 3 //Loop through all the Types found in the assembly 4 foreach (Type pluginType in pluginAssembly.GetTypes()) { 5 //Only look at public and non-abstract types 6 if (pluginType.IsPublic && !pluginType.IsAbstract) { 7 Type typeInterface = null; 8 try { 9 //Gets a type object of the interface we need the plugins to match 10 typeInterface = pluginType.GetInterface("IPlugin", true); 11 } 12 catch (Exception) { } 13 14 //Make sure the interface we want to use actually exists 15 if (typeInterface1 != null) { 16 //Create a new available plugin instance 17 T newPlugin = 18 (T)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); 19 20 availablePlugins.Add(newPlugin); 21 } 22 } 23 } 24 } Here's a method I've got in a generic PluginManager class with parameter T. It adds a plugin of the right type to a list of available plugins. The problem I've got is in line 10. Instead of "IPlugin" (The parent interface of the different interfaces for plugins (IParser, ISolution, etc.), something like "T" should be used. Unfortunatly I couldn't find out how to get the name of the parameter T as a string. ToString and casting doesn't work on the generic parameter. Is there any way of getting the right string ("IParser", "ISolution", etc.) depending on the chosen parameter T?
-
Try looking at the methods in pluginType which have generic in them, e.g. you might want to try
pluginType.GetGenericParameters
(I think it's called that anyway).
I have no idea what I just said. But my intentions were sincere.
I found a solution, but I'm not quite happy with it. I simply pass the string I need to the constructor, when I create an instance of the class. Thanks for the help I'll check it out, it might be the cleaner way of accessing the string I need.