Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

.net - VB.NET dynamic plugin components

I am not a VB developer but I am heading up a large project in which VB is partially used. One of the requirements is to implement a plugin architecture to support dynamically extendable application core.

Our VB developer seems to think its possible to store the BLL in a DLL - keeping the interface all in the original core application - rendering it useless until the extensions are installed.

Obviously this is less than ideal. I would like to know whether its possible to keep the entire sub application/component in a distinct DLL load it into the core platform???

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is not problem at all, take a look at my sample code. Here is abstract class that exists in separate dll:

public abstract class ServerTask
{
    public int TaskId { get; set;}

    /// <summary>
    /// Gets description for ServerTask
    /// </summary>
    public abstract string Description { get; }


}

This is code from plugin implementation:

public class SampleTask : GP.Solutions.WF.Entities.Tasks.ServerTask
{

    public override string Description
    {
        get 
        {
            return "Sample plugin";
        }
    }

}

And finally code from core application that loads plugin:

    /// <summary>
    /// Loads Plugin from file
    /// </summary>
    /// <param name="fileName">Full path to file</param>
    /// <param name="lockFile">Lock loaded file or not</param>
    /// <returns></returns>
    static Entities.Tasks.ServerTask LoadServerTask(string fileName, bool lockFile, int taskId)
    {
        Assembly assembly = null;
        Entities.Tasks.ServerTask serverTask = null;
        if (lockFile)
        {
            assembly = System.Reflection.Assembly.LoadFile(fileName);
        }
        else
        {
            byte[] data = Services.Common.ReadBinaryFile(fileName);
            assembly = System.Reflection.Assembly.Load(data);
        }
        Type[] types = assembly.GetTypes();
        foreach (Type t in types)
        {
            if (t.IsSubclassOf(typeof(Entities.Tasks.ServerTask)))
            {
                serverTask = (Entities.Tasks.ServerTask)Activator.CreateInstance(t);
                serverTask.TaskId = taskId;
                break;
            }
        }
        if (serverTask == null)
        {
            throw new Exception("Unable to initialize to ServerTask type from library '" + fileName + "'!");
        }
        return serverTask;
    }

If you are not familiar with c# just use c# to vb.net online converter.

Happy coding and best regards! Gregor Primar


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...