开发者

Using MEF in an editor

开发者 https://www.devze.com 2023-01-31 02:54 出处:网络
Im trying to developing editor like VS editor where i hawe components like FTP, TelNet (where you drag and drop in designer and connect them with each other change properties in PropertyGrid and so on

Im trying to developing editor like VS editor where i hawe components like FTP, TelNet (where you drag and drop in designer and connect them with each other change properties in PropertyGrid and so on) and then execute. Is good idea to use MEF to scan all dll-s that have interface IComponent and then use reflection to get the original Object from dll-s. example

[Export(typeof(ICOmponent))]
[MetadataExport("Name", "FTP")]
public class FTP : ICOmponent
{
    public string Server { get; set; }
    public void Start()
    {
        ....ConectTOServer(Server);
    }
}
[Export(typeof(ICOmponent))]
[MetadataExport("Name",开发者_运维问答 "MessageBox")]
public class MessageBox : ICOmponent
{
    public string Message { get; set; }
    public void Start()
    {
        System.Windows.Forms.MessageBox.Show(Message);
    }
}

public interface ICOmponent
{
    void Start();
}

Or is another method to get original object from MEF from ICOmponent like from Metadata.. because PropertyGrid nead real object for displaying properties for FTP Server and From MessageBox Message. Sorry for my bad english.


For MEF to work, any assembly/dll that is dropped on you application must implement Exports. If it does not, you won't be able to Import anything.

To work with assembly/dll that does not have any exports, use reflection to search for types that implement IComponent. And use Activator class to create instances from their type information.

0

精彩评论

暂无评论...
验证码 换一张
取 消