Call Constructor Base after Code Execution

2020-06-07 07:39发布

Let say we have Class A and Class B. ClassB extends Class A. (ClassB : ClassA)

Now let's say that whenever I instantiate ClassB, I'd like to Run some Random code and only then call "base" to reach ClassA constructor.

Like:

class ClassA
{
    public ClassA()
    {
        Console.WriteLine("Initialization");
    }  
}

class ClassB : ClassA
{
    public ClassB() //: base() 
    {
        // Using :base() as commented above, I would execute ClassA ctor before                                                         //          Console.WriteLine as it is below this line... 
        Console.WriteLine("Before new");
        //base() //Calls ClassA constructor using inheritance
        //Run some more Codes here...
    }
}

In the programming language I usually work with, I can do that, by simply calling super() after Console.WriteLine(); But I cant make it in C#. Is there any other syntax or other way to do that?

9条回答
SAY GOODBYE
2楼-- · 2020-06-07 07:51

You can't do that with C#. Your best bet is to extract that code into it's own method in the parent and then call that from the child when you're ready.

查看更多
Anthone
3楼-- · 2020-06-07 07:54

C# doesn't allow calling base constructors inside constructor bodies, different from Java.

查看更多
该账号已被封号
4楼-- · 2020-06-07 07:59

Another elegant solution would be to completely rethink how your objects are constructed. In the constructor of your base class you can call your own construct function, and you omit dependent future constructors, in the following way:

public class ClassA
{
    public ClassA()
    {
        Construct();
    }

    public virtual void Construct()
    {
        Console.WriteLine("3");
    }
}

public class ClassB : ClassA
{
    public override void Construct()
    {
        Console.WriteLine("2");
        base.Construct();
    }
}

public class ClassC : ClassB
{
    public override void Construct()
    {
        Console.WriteLine("1");
        base.Construct();
    }
}
查看更多
家丑人穷心不美
5楼-- · 2020-06-07 08:07

I had the same problem. I found this solution to be the best if you don't have access to the base class.

public class BaseClass
{
    public BaseClass(string someValue)
    {
        Console.WriteLine(someValue);
    }
}

public class MyClass : BaseClass
{
    private MyClass(string someValue)
        : base(someValue)
    {
    }

    public static MyClass GetNewInstance(string someValue, bool overrideValue = false)
    {
        if (overrideValue)
        {
            someValue = "42";
        }
        return new MyClass(someValue);
    }
}
查看更多
成全新的幸福
6楼-- · 2020-06-07 08:09

You can not call base constructor. But a different thing is that when you declare an object of derived class both constructor derived and base is called.

    class ClassA
{
    public ClassA()
    {
        Console.WriteLine("Initialization");
    }  
}

class ClassB : ClassA
{
    public ClassB() //: base() 
    {
        // Using :base() as commented above, I would execute ClassA ctor before                                                         //          Console.WriteLine as it is below this line... 
        Console.WriteLine("Before new");
        //base() //Calls ClassA constructor using inheritance
        //Run some more Codes here...
    }
}
void main(string[] args)
    {
      ClassB b = new ClassB();

    }
查看更多
小情绪 Triste *
7楼-- · 2020-06-07 08:11

Actually, you can:

class Foo
{
    public Foo(string s)
    {
        Console.WriteLine("inside foo");
        Console.WriteLine("foo" + s);
    }
}

class Bar : Foo
{
    public Bar(string s) : base(((Func<string>)(delegate ()
    {
        Console.WriteLine("before foo");
        return "bar" + s;
    }))())
    {
        Console.WriteLine("inside bar");
    }
}

class Program
{
    static void Main(string[] args)
    {
        new Bar("baz");
    }
}

Output:

before foo
inside foo
foobarbaz
inside bar

But I will prefer to not use this trick if it is possible.

查看更多
登录 后发表回答