简体   繁体   English

在.NET中如何在没有接口继承的情况下在静态类中实现接口方法?

[英]How can I implement interface methods in the static class without Interface Inheritance in .NET?

Interface : 介面

public interface IArrayOperation
{
    int GetElement(int index);        
    bool IndexCheck(int index);
}

Static Class: 静态类:

public static class TestArray
{
    public static int GetArrayLength(IArrayOperation arrayOperation)
    {
        // Implement your logic here.
        // I need to implement interface method over here.
        throw new NotImplementedException();
    }
}

Here, I want to implement both interface methods in the static class method GetArrayLength() . 在这里,我想在静态类方法GetArrayLength()实现这两个接口方法。

I don't want to implement interface but I have passed the interface as a parameter in the static class method. 我不想实现接口,但是我已经将接口作为参数传递给了静态类方法。

Appreciated any help or guidance. 感谢任何帮助或指导。

You cannot implement interface methods without having a derived class. 如果没有派生类,则无法实现接口方法。 However, you can add derived information to an interface by means of extension methods, if your interface provides enough basic functionality. 但是,如果接口提供了足够的基本功能,则可以通过扩展方法将派生的信息添加到接口。

For an array, you could have the interface method IndexCheck and derive the array length by checking for the last valid index. 对于数组,您可以使用接口方法IndexCheck并通过检查最后一个有效索引来得出数组长度。

public interface IArrayOperation
{       
    bool IndexCheck(int index);
}
public static class TestArray
{
    public static int GetArrayLength(this IArrayOperation arrayOperation)
    {
        int len = 0;
        while (arrayOperation.IndexCheck(len)) { ++len; }
        return len;
    }
}

Or you could have an array length and derive the index check 或者您可以具有数组长度并派生索引检查

public interface IArrayOperation
{       
    int GetArrayLength();
}
public static class TestArray
{
    public static bool IndexCheck(this IArrayOperation arrayOperation, int index)
    {
        return index >= 0 && index < arrayOperation.GetArrayLength();
    }
}

In both cases, you can later use an IArrayOperation variable with both methods 在这两种情况下,您以后都可以将IArrayOperation变量与这两种方法一起使用

IArrayOperation instance = /* some concrete derived class */;
bool checkResult = instance.IndexCheck(0);
int lengthResult = instance.GetArrayLength();

Your derived class instances need to implement the methods that are actually part of the interface, but the extension methods are available without implementation per instance. 您的派生类实例需要实现实际上是接口一部分的方法,但是可以使用扩展方法而无需为每个实例实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在接口上实现 static 方法? - How can I implement static methods on an interface? .NET类接口,继承和库:错误未实现接口成员 - .NET Class Interface, Inheritance and Library: error does not implement interface member 我如何使用:Interface实现类? - How can i implement a class using :Interface? 一个类如何在不实现功能之一的情况下实现接口? - How Can a Class Can Implement Interface without implement one Of the Functions? 接口继承-从父接口隐藏方法-为什么在实现类中可以同时使用这两种方法? - Interface inheritance - hiding method from parent interface - why can I have both methods in the implementing class? 我如何在 inheritance 链中多次实现该接口的同时显式实现该接口? - How can i explicitly implement an interface while having implemented that interface multiple times in inheritance chain? 我可以使用扩展方法来实现接口吗? - Can I use Extension Methods to implement an Interface? 接口和类有什么区别,为什么我可以在类中直接实现方法时使用接口? - What is the difference between an interface and a class, and why I should use an interface when I can implement the methods directly in the class? 如何实现接口并从基类继承? - How can I implement an interface and inherit from a base class? 如何使用显式类实现接口类型 - How can I implement an interface type using a explicit class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM