I'm trying to learn how to create plugins, the only problem is my friend only knows .NET, I know C# and can partially read .NET. Is there anyone in this section that can convert this all over to C# for me so I can truly study it?

Public Interface IPlugin
    Sub Initialize(ByVal Host As IHost)
    ReadOnly Property Name() As String
    Function Calculate(ByVal int1 As Integer, ByVal int2 As Integer) As Double
End Interface

Public Interface IHost
    Sub ShowFeedback(ByVal strFeedback As String)
End Interface

Public Class Class1
    Implements PluginSample.Interfaces.IPlugin
    Private objHost As PluginSample.Interfaces.IHost
    Public Sub Initialize(ByVal Host As PluginSample.Interfaces.IHost) _
    Implements PluginSample.Interfaces.IPlugin.Initialize
        objHost = Host
    End Sub
    Public ReadOnly Property Name() As String Implements _
    PluginSample.Interfaces.IPlugin.Name
        Get
            Return "Example Plugin 1 - Adds two numbers"
        End Get
    End Property
    Public Function Calculate(ByVal int1 As Integer, ByVal int2 As Integer) As Double _
    Implements PluginSample.Interfaces.IPlugin.Calculate
        Return int1 + int2
    End Function
End Class

Public Shared Function FindPlugins(ByVal strPath As String, ByVal strInterface As _
    String) As AvailablePlugin()
        Dim Plugins As ArrayList = New ArrayList()
        Dim strDLLs() As String, intIndex As Integer
        Dim objDLL As [Assembly]
        'Go through all DLLs in the directory, attempting to load them
        strDLLs = Directory.GetFileSystemEntries(strPath, "*.dll")
        For intIndex = 0 To strDLLs.Length - 1
            Try
                objDLL = [Assembly].LoadFrom(strDLLs(intIndex))
                ExamineAssembly(objDLL, strInterface, Plugins)
            Catch e As Exception
                'Error loading DLL, we don't need to do anything special
            End Try
        Next
        'Return all plugins found
        Dim Results(Plugins.Count - 1) As AvailablePlugin
        If Plugins.Count <> 0 Then
            Plugins.CopyTo(Results)
            Return Results
        Else
            Return Nothing
        End If
    End Function

Private Shared Sub ExamineAssembly(ByVal objDLL As [Assembly], _
    ByVal strInterface As String, ByVal Plugins As ArrayList)
        Dim objType As Type
        Dim objInterface As Type
        Dim Plugin As AvailablePlugin
        'Loop through each type in the DLL
        For Each objType In objDLL.GetTypes
            'Only look at public types
            If objType.IsPublic = True Then
                'Ignore abstract classes
                If Not ((objType.Attributes And TypeAttributes.Abstract) = _
                TypeAttributes.Abstract) Then
                    'See if this type implements our interface
                    objInterface = objType.GetInterface(strInterface, True)
                    If Not (objInterface Is Nothing) Then
                        'It does
                        Plugin = New AvailablePlugin()
                        Plugin.AssemblyPath = objDLL.Location
                        Plugin.ClassName = objType.FullName
                        Plugins.Add(Plugin)
                    End If
                End If
            End If
        Next
    End Sub

Public Shared Function CreateInstance(ByVal Plugin As AvailablePlugin) As Object
        Dim objDLL As [Assembly]
        Dim objPlugin As Object
        Try
            'Load dll
            objDLL = [Assembly].LoadFrom(Plugin.AssemblyPath)
            'Create and return class instance
            objPlugin = objDLL.CreateInstance(Plugin.ClassName)
        Catch e As Exception
            Return Nothing
        End Try
        Return objPlugin
    End Function

All help is greatly appreciated. I'm just trying to learn something new and can't truly understand .NET.

Recommended Answers

All 4 Replies

Here is a VB -> C# or viceversa converter

This is the result:

public interface IPlugin
{
  void Initialize(IHost Host);
  string Name {
    get;
  }
  double Calculate(int int1, int int2);
}

public interface IHost
{
  void ShowFeedback(string strFeedback);
}

public class Class1 : PluginSample.Interfaces.IPlugin
{
  private PluginSample.Interfaces.IHost objHost;
  public void PluginSample.Interfaces.IPlugin.Initialize(PluginSample.Interfaces.IHost Host)
  {
    objHost = Host;
  }
  public string PluginSample.Interfaces.IPlugin.Name {
    get { return "Example Plugin 1 - Adds two numbers"; }
  }
  public double PluginSample.Interfaces.IPlugin.Calculate(int int1, int int2)
  {
    return int1 + int2;
  }
}

public static AvailablePlugin[] FindPlugins(string strPath, string strInterface)
{
  ArrayList Plugins = new ArrayList();
  string[] strDLLs;
  int intIndex;
  Assembly objDLL;
  //Go through all DLLs in the directory, attempting to load them
  strDLLs = Directory.GetFileSystemEntries(strPath, "*.dll");
  for (intIndex = 0; intIndex <= strDLLs.Length - 1; intIndex++) {
    try {
      objDLL = Assembly.LoadFrom(strDLLs(intIndex));
      ExamineAssembly(objDLL, strInterface, Plugins);
    }
    catch (Exception e) {
      //Error loading DLL, we don't need to do anything special
    }
  }
  //Return all plugins found
  AvailablePlugin[] Results = new AvailablePlugin[Plugins.Count - 1];
  if (Plugins.Count != 0)
  {
    Plugins.CopyTo(Results);
    return Results;
  }
  else
  {
    return null;
  }
}

private static void ExamineAssembly(Assembly objDLL, string strInterface, ArrayList Plugins)
{
  Type objType;
  Type objInterface;
  AvailablePlugin Plugin;
  //Loop through each type in the DLL
  foreach ( objType in objDLL.GetTypes) {
    //Only look at public types
    if (objType.IsPublic == true)
    {
      //Ignore abstract classes
      if (!((objType.Attributes & TypeAttributes.Abstract) == TypeAttributes.Abstract))
      {
        //See if this type implements our interface
        objInterface = objType.GetInterface(strInterface, true);
        if (!(objInterface == null))
        {
          //It does
          Plugin = new AvailablePlugin();
          Plugin.AssemblyPath = objDLL.Location;
          Plugin.ClassName = objType.FullName;
          Plugins.Add(Plugin);
        }
      }
    }
  }
}

public static object CreateInstance(AvailablePlugin Plugin)
{
  Assembly objDLL;
  object objPlugin;
  try {
    //Load dll
    objDLL = Assembly.LoadFrom(Plugin.AssemblyPath);
    //Create and return Class instance
    objPlugin = objDLL.CreateInstance(Plugin.ClassName);
  }
  catch (Exception e) {
    return null;
  }
  return objPlugin;
}

Hope this helps

If it works, don't convert it; just make it a class library and you can call it from any .NET language (including PowerShell).

This is .NET, so take advantage of it.

Well I'm trying to learn how to make my own plugins for applications in C#. I would like them to be used via open file dialog. I'll give it a try and find out if it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.