load all plugin sample dynamically.

plugin的load:

1. parameters load the plugin, which implement the interface IplugIn.

2. via reflector GetTypes(), get its type name,

3. create instance for the plugin,

so that we can load our all kinds of plugin dynamically.


private void LoadObjects(Assembly assembly)
        {
            IEnumerable<Type> types = null;
            try
            {
                types = from type in assembly.GetTypes()
                        where type.IsClass && (type.GetInterface(typeof(IPlugIn).Name) != null)
                        select type;
            }
            catch (ReflectionTypeLoadException)
            {
                logger.TraceEvent(TraceEventType.Warning,
                                  SREvents.InvalidAssemblyProcessedId,
                                  SREvents.InvalidAssemblyProcessed(assembly.FullName));
                return;
            }

            foreach (Type IPlugInType in types)
            {
                IPlugIn plugIn = (IPlugIn)assembly.CreateInstance(IPlugInType.FullName, true);
                GetPluginAttributesCreateInstance(plugIn, assembly, IPlugInType);
            }
        }




你可能感兴趣的:(load all plugin sample dynamically.)