Call constructor in abstract class with generics

mosquito87

I have the following base class which is abstract:

public abstract class MasterDataViewModel<TPrimaryModel> : ViewModel
    where TPrimaryModel : TwoNames, new()
{
    ...
}

I have some derived classes like:

public class ConsignorViewModel : MasterDataViewModel<Consignor>
{
    protected override void Find()
    {           
        ...
        foreach (Consignor consignor in consignors)
        {
            ConsignorViewModel viewModel = new ConsignorViewModel(consignor, consignor.Address);
            SearchResults.Add(viewModel);
        }
        ...

    }
}

I would like to move the Find method to my base class. The problem is the constructor call which is not possible in my abstract class. It has to be something like this:

protected override void Find()
{         
    ...
    foreach (TPrimaryModel primaryModel in results)
    {
        MasterDataViewModel<TPrimaryModel> viewModel = new MasterDataViewModel(primaryModel, primaryModel.Address);
        SearchResults.Add(viewModel);
    }
    ...
}

It's not working as constructor calls in abstract classes are not allowed. Any other solution? I thought about another generic. Problem is that a new constraint cannot have any parameters so this doesn't work, too.

Tim Eeckhaut

Either create an abstract method in the base class "CreateViewModel" that you override in each model. Or use the Activator.CreateInstance method to create an instance of the this.GetType() subclass.

The first one is the cleanest. That way your class serves as a factory for itself.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call constructor in an abstract class

From Dev

Constructor to interface/abstract class using Java generics

From Dev

Abstract class with constructor, force inherited class to call it

From Dev

java call method in constructor and abstract class?

From Dev

Abstract class constructor call overridable method

From Java

Java Generics Call Constructor

From Dev

How does abstract class call get a child value in constructor?

From Dev

How do I simulate an abstract class and call its constructor?

From Java

Java abstract class, abstract constructor

From Dev

How do I create a constructor for a class that is inheriting from an abstract class with generics?

From Dev

Generics and constructor in the inherited class

From Java

Private constructor in abstract class

From Dev

Java constructor of an abstract class

From Java

Copy constructor for abstract class

From Dev

How to call the constructor of an Abstract class from another constructor inside the same class (method overloading)

From Dev

How would I call a constructor of an abstract class inside of an extending class constructor

From Dev

How to extends an abstract class with generics?

From Dev

Java abstract class Implementing with generics

From Java

Abstract methods in an abstract class constructor: design flaw?

From Dev

Inheriting class with required constructor in generics

From Java

How to call super constructor with params from abstract class if current class is spring bean?

From Dev

Instance of abstract class with hidden constructor

From Java

Constructor injection on abstract class and children

From Dev

Mocking an abstract method, but not the class constructor

From Dev

Java constructor of a subclass of an abstract class

From Java

Abstract class constructor access modifier

From Java

Can an abstract class have a constructor?

From Java

abstract class vs private constructor

From

Constructor of an abstract class in C#

Related Related

  1. 1

    Call constructor in an abstract class

  2. 2

    Constructor to interface/abstract class using Java generics

  3. 3

    Abstract class with constructor, force inherited class to call it

  4. 4

    java call method in constructor and abstract class?

  5. 5

    Abstract class constructor call overridable method

  6. 6

    Java Generics Call Constructor

  7. 7

    How does abstract class call get a child value in constructor?

  8. 8

    How do I simulate an abstract class and call its constructor?

  9. 9

    Java abstract class, abstract constructor

  10. 10

    How do I create a constructor for a class that is inheriting from an abstract class with generics?

  11. 11

    Generics and constructor in the inherited class

  12. 12

    Private constructor in abstract class

  13. 13

    Java constructor of an abstract class

  14. 14

    Copy constructor for abstract class

  15. 15

    How to call the constructor of an Abstract class from another constructor inside the same class (method overloading)

  16. 16

    How would I call a constructor of an abstract class inside of an extending class constructor

  17. 17

    How to extends an abstract class with generics?

  18. 18

    Java abstract class Implementing with generics

  19. 19

    Abstract methods in an abstract class constructor: design flaw?

  20. 20

    Inheriting class with required constructor in generics

  21. 21

    How to call super constructor with params from abstract class if current class is spring bean?

  22. 22

    Instance of abstract class with hidden constructor

  23. 23

    Constructor injection on abstract class and children

  24. 24

    Mocking an abstract method, but not the class constructor

  25. 25

    Java constructor of a subclass of an abstract class

  26. 26

    Abstract class constructor access modifier

  27. 27

    Can an abstract class have a constructor?

  28. 28

    abstract class vs private constructor

  29. 29

    Constructor of an abstract class in C#

HotTag

Archive