OpenSim/Server/Base/ServerUtils.cs
/// <summary>
/// Load a plugin from a dll with the given class or interface
/// </summary>
/// <param name="dllName"></param>
/// <param name="className"></param>
/// <param name="args">The arguments which control which constructor is invoked on the plugin</param>
/// <returns></returns>
Load a plugin from a dll with the given class or interface
Load a plugin from a dll with the given class or interface
public static T LoadPlugin<T>(string dllName, string className, Object[] args) where T:class
        {
            string interfaceName = typeof(T).ToString();
            try
            {
                Assembly pluginAssembly = Assembly.LoadFrom(dllName);
                foreach (Type pluginType in pluginAssembly.GetTypes())
                {
                    if (pluginType.IsPublic)
                    {
                        if (className != String.Empty 
                            && pluginType.ToString() != pluginType.Namespace + "." + className)
                            continue;
                        Type typeInterface = pluginType.GetInterface(interfaceName, true);
                        if (typeInterface != null)
                        {
                            T plug = null;
                            try
                            {
                                plug = (T)Activator.CreateInstance(pluginType,
                                        args);
                            }
                            catch (Exception e)
                            {
                                if (!(e is System.MissingMethodException))
                                {
                                    m_log.Error(string.Format("[SERVER UTILS]: Error loading plugin {0} from {1}. Exception: {2}",
                                        interfaceName, 
                                        dllName, 
                                        e.InnerException == null ? e.Message : e.InnerException.Message),
                                            e);
                                }
                                m_log.ErrorFormat("[SERVER UTILS]: Error loading plugin {0}: {1} args.Length {2}", dllName, e.Message, args.Length);
                                return null;
                            }
                            return plug;
                        }
                    }
                }
                return null;
            }
            catch (ReflectionTypeLoadException rtle)
            {
                m_log.Error(string.Format("[SERVER UTILS]: Error loading plugin from {0}:\n{1}", dllName,
                    String.Join("\n", Array.ConvertAll(rtle.LoaderExceptions, e => e.ToString()))),
                    rtle);
                return null;
            }
            catch (Exception e)
            {
                m_log.Error(string.Format("[SERVER UTILS]: Error loading plugin from {0}", dllName), e);
                return null;
            }
        }
public static T LoadPlugin < T > ( string dllName , string className , Object [ ] args ) where T : class { string interfaceName = typeof ( T ) . ToString ( ) ; try { Assembly pluginAssembly = Assembly . LoadFrom ( dllName ) ; foreach ( Type pluginType in pluginAssembly . GetTypes ( ) ) { if ( pluginType . IsPublic ) { if ( className != String . Empty && pluginType . ToString ( ) != pluginType . Namespace + " " + className ) continue ; Type typeInterface = pluginType . GetInterface ( interfaceName , true ) ; if ( typeInterface != null ) { T plug = null ; try { plug = ( T ) Activator . CreateInstance ( pluginType , args ) ; } catch ( Exception e ) { if ( ! ( e is System . MissingMethodException ) ) { m_log . Error ( string . Format ( " " , interfaceName , dllName , e . InnerException == null ? e . Message : e . InnerException . Message ) , e ) ; } m_log . ErrorFormat ( " " , dllName , e . Message , args . Length ) ; return null ; } return plug ; } } } return null ; } catch ( ReflectionTypeLoadException rtle ) { m_log . Error ( string . Format ( " \n " , dllName , String . Join ( " \n " , Array . ConvertAll ( rtle . LoaderExceptions , e => e . ToString ( ) ) ) ) , rtle ) ; return null ; } catch ( Exception e ) { m_log . Error ( string . Format ( " " , dllName ) , e ) ; return null ; } }
Load a plugin from a dll with the given class or interface
Load a plugin from a dll with the given class or interface
None
dllName
string
None
className
string
None
args
Object[]
The arguments which control which constructor is invoked on the plugin
The arguments which control which constructor is invoked on the plugin