How to override a method when the base class has no defined constructors

I am trying to override a method from a base class that is a third party DLL.

I am getting an exception:

type 'B' has no defined constructors.

Is there a way to override a method in a derived class?

Below is a code snippet.

Public Class A : B
{
   Public A()
   {
   }       

   // method to override from Class B
   public override object BaseMethod() 
   {
   }
}

      


I also checked Type '...' has no constructors defined , but it doesn't talk about inheritance, but rather just updates the instance.

Inheriting an abstract class without any constructor explains that I cannot inherit from such a class - so look for another way to override the method.

+3


source to share


1 answer


As explained in Inherit an abstract class without any constructor, you cannot derive from a class that does not provide constructors. Hiding constructors are often done explicitly as a way to prevent inheritance when sealed

not an option. So there is a likely reason why you cannot do this for this particular type.

Options:



  • modify (or request) DLL to expand constructors
  • see if you really need to extract from this particular class or if there is an interface your code can implement
  • derives from some other class, if it exists (for example C:B

    ), which is also mapped from the DLL but has constructors available. Please note that unless the class hierarchy is carefully designed, such classes will not be available.

Alternatively, you can ask the authors of the assembly how to solve your problem in some other way (chances are good they have solutions for common scenarios they already thought of).

+4


source







All Articles