Feedback

Simple Pluginloader

Sprache: C#

This is a simple pluginloader. example: [code] Runtime.CreateRuntime(Application.StartupPath + "\Plugin.dll"); var plugs = Runtime.CreateMultipleInstance<Plugin>(); foreach (var p in plugs) { listBox1.Items.Add(p.GetType().Name); } [/code] [code] private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { var p = Runtime.CreateInstanceOf<Plugin>(listBox1.SelectedItem.ToString()); p.Host = this; p.OnClick(); }[/code]
class Runtime
    {

        private static Assembly assembly;

        public static void CreateRuntime(string ass)
        {
            assembly = Assembly.LoadFile(ass);
        }

        [DebuggerStepThrough]
        public static t CreateInstance<t>()
        {
            object returns = null;

            foreach (var ty in assembly.GetTypes())
            {
                if (ty.BaseType == typeof(t))
                {
                    returns = assembly.CreateInstance(ty.FullName);
                }
            }

            if (returns != null)
            {
                return (t)returns;
            }
            else
            {
                throw new Exception("Konnte keine Instanz vom Typ '" + typeof(t).Name + "' erstellen");
            }
        }

        public static Assembly GetAssembly()
        {
            return assembly;
        }

        [DebuggerStepThrough]
        public static t[]  CreateMultipleInstance<t>()
        {
            List<t> returns = new List<t>();

            foreach (var ty in assembly.GetTypes())
            {
                if (ty.BaseType == typeof(t))
                {
                    returns.Add((t) assembly.CreateInstance(ty.FullName));
                }
            }

            if (returns.Count != 0)
            {
                return returns.ToArray();
            }
            else
            {
                throw new Exception("Konnte keine Instanz vom Typ '" + typeof(t).Name + "' erstellen");
            }
        }

        [DebuggerStepThrough]
        public static t CreateInstanceOf<t>(string classname)
        {
            object returns = null;

            foreach (var ty in assembly.GetTypes())
            {
                if (ty.Name == classname)
                {
                    returns = assembly.CreateInstance(ty.FullName);
                }
            }

            if (returns != null)
            {
                return (t)returns;
            }
            else
            {
                throw new Exception("Konnte keine Instanz vom Typ '" + typeof(t).Name + "' erstellen");
            }
        }

    }
class Runtime
    {

        private static Assembly assembly;

        public static void CreateRuntime(string ass)
        {
            assembly = Assembly.LoadFile(ass);
        }

        [DebuggerStepThrough]
        public static t CreateInstance<t>()
        {
            object returns = null;

            foreach (var ty in assembly.GetTypes())
            {
                if (ty.BaseType == typeof(t))
                {
                    returns = assembly.CreateInstance(ty.FullName);
                }
            }

            if (returns != null)
            {
                return (t)returns;
            }
            else
            {
                throw new Exception("Konnte keine Instanz vom Typ '" + typeof(t).Name + "' erstellen");
            }
        }

        public static Assembly GetAssembly()
        {
            return assembly;
        }

        [DebuggerStepThrough]
        public static t[]  CreateMultipleInstance<t>()
        {
            List<t> returns = new List<t>();

            foreach (var ty in assembly.GetTypes())
            {
                if (ty.BaseType == typeof(t))
                {
                    returns.Add((t) assembly.CreateInstance(ty.FullName));
                }
            }

            if (returns.Count != 0)
            {
                return returns.ToArray();
            }
            else
            {
                throw new Exception("Konnte keine Instanz vom Typ '" + typeof(t).Name + "' erstellen");
            }
        }

        [DebuggerStepThrough]
        public static t CreateInstanceOf<t>(string classname)
        {
            object returns = null;

            foreach (var ty in assembly.GetTypes())
            {
                if (ty.Name == classname)
                {
                    returns = assembly.CreateInstance(ty.FullName);
                }
            }

            if (returns != null)
            {
                return (t)returns;
            }
            else
            {
                throw new Exception("Konnte keine Instanz vom Typ '" + typeof(t).Name + "' erstellen");
            }
        }

    }