Call Constructor Base after Code Execution

后端 未结 9 2002
攒了一身酷
攒了一身酷 2021-02-12 12:33

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 o

相关标签:
9条回答
  • 2021-02-12 13:14

    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();
    
        }
    
    0 讨论(0)
  • 2021-02-12 13:16

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

    0 讨论(0)
  • 2021-02-12 13:19

    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);
        }
    }
    
    0 讨论(0)
  • 2021-02-12 13:22

    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.

    0 讨论(0)
  • 2021-02-12 13:23

    Another hack if you can get away with calling a static method.

    public class ClassA
    {
        public ClassA()
        {
            Debug.WriteLine("Call A Constructor");
        }
    }
    
    public class ClassB:ClassA
    {
        public ClassB():this(aMethod())
        {
        }
    
        private ClassB(object empty):base()
        {
            Debug.WriteLine("Class B Second Constructor");
        }
    
        private static object aMethod()
        {
            Debug.WriteLine("Run me First");
            return null;
        }
    }
    
    0 讨论(0)
  • 2021-02-12 13:25

    There's a hacky way of doing it using an instance variable initializer:

    using System;
    
    class ClassA
    {
        public ClassA()
        {
            Console.WriteLine("Initialization");
        }  
    }
    
    class ClassB : ClassA
    {
        private readonly int ignoreMe = BeforeBaseConstructorCall();
    
        public ClassB()
        {
        }
    
        private static int BeforeBaseConstructorCall()
        {
            Console.WriteLine("Before new");
            return 0; // We really don't care
        }
    }
    
    class Test
    {
        static void Main()
        {
            new ClassB();
        }    
    }
    

    The less hacky way of doing it is to rethink how you construct a ClassB to start with. Instead of having clients call the constructor directly, provide a static method for them to call:

    public static ClassB CreateInstance()
    {
        Console.WriteLine("Before initialization stuff");
        return new ClassB();
    }
    
    0 讨论(0)
提交回复
热议问题