Howdie!
So I'm deserializing a class called Method using .NET Serialization. Method contains a list of objects implementing IAction. I originally used the [XmlInclude] attribute to specify all classes which implement IAction.
But now, I'd like to change my program to load all dlls in a directory and strip out the classes which implement IAction. Then users can deserialize files which contain their actions implementing IAction.
But I don't control the classes which implement IAction anymore, and so I can't use XmlInclude :( Is there a way to set this attribute at runtime? Or have a similar attribute set for the implementing class?
public class Method
{
public List<Actions.IAction> Actions = new List<Actions.IAction>();
}
public interface IAction
{
void DoExecute();
}
public static Type[] LoadActionPlugins(string pluginDirectoryPath)
{
List<Type> pluginTypes = new List<Type>();
string[] filesInDirectory = Directory.GetFiles(pluginDirectoryPath, "*.dll", SearchOption.TopDirectoryOnly);
foreach (string pluginPath in filesInDirectory)
{
System.Reflection.Assembly actionPlugin = System.Reflection.Assembly.LoadFrom(pluginPath);
Type[] assemblyTypes = actionPlugin.GetTypes();
foreach (Type type in assemblyTypes)
{
Type foundInterface = type.GetInterface("IAction");
if (foundInterface != null)
{
pluginTypes.Add(type);
}
}
}
return pluginTypes.Count == 0 ? null : pluginTypes.ToArray();
}