简体   繁体   中英

Override at least one method

Let's suppose there is a following class:

public class foo{
    internal virtual object M1(/*args*/){return null;}
    internal virtual object[] M2(/*args*/){return null;}

    public SomeStruct SomeMethod(){
        return new SomeStruct
        {
            Obj = M1();
            ObjArr = M2();
        }
    }
}

Using the following struct:

public class SomeStruct
{
    public object Obj;
    public object[] ObjArr;
}

Is there a way to make sure (preferably at compilation) to force either at least one method or exactly one method of class foo to be overriden?

Before anybody says it - I know it's possible to use one method and check if the result is array (or IEnumerable) and then assign it to the right field, but that takes more time then just running empty methods. I'm just wondering if it's possible to do it that way.

You could mark the methods abstract , and then you will be forced to implement both of the methods. This seems to be the most straightforward solution:

internal abstract object M1(/*args*/){return null;}
internal abstract object[] M2(/*args*/){return null;}

Another option, actually too complicated for this purpose, is to write a Roslyn code analyzer which will check the code and determines if it is valid.

As a side note: your fields should reside in the base class too. You could use generics if you want to make the types of them generic.

"No", basically. At least, not without writing your own custom code analyzer (perhaps via Roslyn), and considering what happens if X : foo overrides M1 , and Y : X overrides M2 .

You need to create a new method in your child class that hides the implementation of the base class, with the new return type. You cannot use virtual methods to overload a method like you're doing.Overloading of methods is done by changing the parameters, not the return type.

So either hide the parent method, in the child class, or create a method with another name.

Here is something that I can think of, just an example.

Run it here .Net Fiddle

using System;

public class a
{
    public virtual object s(int a)
    {
        return a + 1;
    }
}

public class b : a
{
    public virtual object[] s(int a)
    {
        var arr = new object[]{a + 2};
        return arr;
    }
}

public class c : b
{
    private a A = new a();
    private b B = new b();
    public c()
    {
        print(2);
    }

    public void print(int a)
    {
        var result = A.s(1);
        Console.WriteLine("result : " + result);

        var resultB = B.s(1);
        //or resultB = base.s(1);

        foreach (var r in resultB)
        {
            Console.WriteLine("result B : " + r);
        }
    }
}

public class Program
{
    public static void Main()
    {
        c C = new c();
    }
}

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