通过接口调用方法而不创建接口的新实例

时间:2015-06-24 08:29:23

标签: c# .net object interface

我想要做的其实很简单,但由于某种原因,我要么采取错误的方式或者只是缺少一些重要的知识,因为我无法弄清楚如何通过接口调用方法无需创建类/接口的新实例。

无论哪种方式,我想要实现的是通过实现该方法的接口调用以下方法,而不必创建新实例。

public partial class MyClass : IMyInterface
{
   public void MyMethod(object obj)
   {
      //Method code....
   }
 }

我的界面

public interface IMyInterface
{
   void MyMethod(object obj);
}

我的解决方案有三个项目

  • 数据
  • 服务
  • UI

界面和MyClass.xaml.cs在UI中,我试图通过使用现有的界面实例从我的服务层中的类调用MyMethod()方法。

我是从代码中的另一部分这样做的,这里不起作用,我不知道为什么

//Register interface
internal static IMyInterface mi;
//and call it like so
mi.MyMethod(Object);

我做了很多谷歌搜索,但似乎每个人都创建了一个新的实例,在我看来这不是一个有效的选项。

4 个答案:

答案 0 :(得分:3)

  

我想要实现的是通过以下方法调用以下方法   实现该方法的接口,无需创建新的   实例

实现方法的

接口 - 是一个错误的假设。接口永远不会实现该方法。它只是说实现这个接口的人应该实现该方法。

实现您的界面的类 - 在您的情况下MyClass。您需要创建它的实例。

internal IMyInterface mi = new MyClass();
mi.MyMethod(Object);

没有别的办法。

答案 1 :(得分:1)

您无法创建界面实例。您可以通过它实现的接口访问对象。例如,如果您有List<T>,则可以通过IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>访问它。

接口没有任何代码或任何实现。您定义了IFruit这样的界面,该界面具有ColorTaste属性以及Eat方法,但您没有在其中实现任何内容。当你说Red Apple类正在实现IFruit时,你实现了该类中的属性和方法。

换句话说,您可以通过接口访问对象。您必须在界面后面有一个对象。

答案 2 :(得分:1)

  

使用现有的接口实例。

因此,如果您创建了一个MyClass类实例,则可以为其Singleton实现like this。因此,您的单个实例将随处可用。

例如:

public partial class MyClass : IMyInterface
{
    private static IMyInterface _instance;

    public static IMyInterface Instance
    { 
        get 
        {
            if (_instance == null) _instance = new MyClass();
            return _instance;
        }
    }
    public void MyMethod(object obj)
    {
       //Method code....
    }
}

//Register interface
internal static IMyInterface mi = MyClass.Instance;
//and call it like so
mi.MyMethod(Object);

答案 3 :(得分:0)

去看Necro吧。

我将提供一种替代方法/路径,它提供了一种不同的方法来实现和执行接口

.Net Fiddle(如果清除短网址,则包含内容)

设置

接口

public interface IMyInterface{
    void MyMethod();
}

public class MyClass : IMyInterface{
    // Implicit Implementation
    public void MyMethod(){
        Console.WriteLine("* Entered the Implicit Interface Method");

        (this as IMyInterface).MyMethod();
    }

    // Explicit Implementation
    void IMyInterface.MyMethod(){
        Console.WriteLine("--> Entered the Explicit Interface Method");
    }
}

执行

using System;

public class Program
{
    public static void Main()
    {
        var cl = new MyClass();

        Console.WriteLine("Executing method off Object");
        Console.WriteLine("--------------------------------");

        cl.MyMethod();

        Console.WriteLine("");
        Console.WriteLine("");
        Console.WriteLine("Executing method through Casting");
        Console.WriteLine("--------------------------------");

        (cl as IMyInterface).MyMethod();

    }
}

结果

Executing method off Object
--------------------------------
* Entered the Implicit Interface Method
--> Entered the Explicit Interface Method


Executing method through Casting
--------------------------------
--> Entered the Explicit Interface Method