简体   繁体   中英

Interface class in C#

In my desktop application, I am facing a problem in using the function in Interface class.

I have a function like this for executing plugin

public static string ExecutePugin(string PluginName, string ConnectionString)
{
    //ToDo: Get the plugin dll in the memory in a different appdomain. call RunAnalysis method of that 
    //ToDo: shift the primary key checking method to inside the plugin and return the result back. 

    //Loads the IMFDBAnalyserPlugin.exe to the current application domain.
    AppDomain.CurrentDomain.Load("IMFDBAnalyserPlugin");

    // Load the plugin's assembly to the current application doamin.
    Assembly oAssembly = AppDomain.CurrentDomain.Load(PluginName);

    // This block of code will execute the plugin's assembly code.
    foreach (Type oType in oAssembly.GetTypes())
    {
        if (oType.GetInterface("IMFDBAnalyserPlugin") != null)
        {
            object oPlugin = Activator.CreateInstance(oType, null, null);
            ((MFDBAnalyser.IMFDBAnalyserPlugin)oPlugin).ExecutePlugin();
        }
    }
    return string.Empty;
}

where IMFDBAnalyserPlugin class is an interface and contains code like this

using System;
using System.Collections.Generic;

using System.Linq;
using System.Text;

namespace MFDBAnalyser
{
    public class IMFDBAnalyserPlugin
    {
        void ExecutePlugin();
    }
}

but on building the project I am getting the error in MFDBAnalyser.IMFDBAnalyserPlugin as

Error 1 The type name 'IMFDBAnalyserPlugin' does not exist in the type 'MFDBAnalyser.MFDBAnalyser' D:\\Projects\\Mindfire\\GoalPlan\\MFDBAnalyser\\MFDBAnalyser\\PluginManager.cs 57 107 MFDBAnalyser

can anyone help me

Are you including MFDBAnalyser in the usings in the main class?

Something like

using MFDBAnalyser; 

in PluginManager ?

Also,

you should change

public class IMFDBAnalyserPlugin 
{ 
    void ExecutePlugin(); 
} 

to

public interface IMFDBAnalyserPlugin 
{ 
    void ExecutePlugin(); 
} 

Have a look at interface (C# Reference)

namespace MFDBAnalyser
{
    interface IMFDBAnalyserPlugin
    {
        void ExecutePlugin();
    }
}

Otherwise oType.GetInterface("IMFDBAnalyserPlugin") will be always null because there is no such interface there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM