C# Reflection: Is it possible to find an instance of an object at runtime?

前端 未结 7 1109
说谎
说谎 2020-12-07 01:53

I\'m wondering if it\'s possible to use reflection to locate an object at runtime? This is more of an experiment than a practical requirement.

I\'ve used the .GetTyp

7条回答
  •  甜味超标
    2020-12-07 02:25

    You can design a plugin framework for your application. Here's an example:

    public interface IPlugin
    {
    void Load(Form mainForm); //Or you can have an interface for you main form that allows your plugin to work with your form.
    }
    

    then you can find your plugins when you load an assembly at run time.

    foreach(var type in assembly.GetTypes())
    {
    if(typeof(IPlugin).IsAssignableFrom(type))
    var plugin=(IPlugin)Activator.CreateInstance(type);
    plugin.Load(_mainForm);
    }
    

    Updatd: BTW as far as I know the answer to your question is no

提交回复
热议问题