How can I make order irrelevant when calling fluent methods of inherited objects?

Deane

When extending a class with a fluent method, the order you call the fluent methods matters, due to inheritance and the type of the returned object. A fluent method from the base will return an object of the base type, which won't have any fluent methods from the inherited type. But reverse the method calls and you're fine.

For example:

public class Thing
{
  public Thing DoThing()
  {
    return this;
  }
}

public class ExtendedThing : Thing
{
  public ExtendedThing DoExtendedThing()
  {
    return this;
  }
}

// This works.
// DoExtendedThing returns an ExtendedThing object,
// which gets DoThing from Thing via inheritance
var extendedThing1 = new ExtendedThing().DoExtendedThing().DoThing();

// This doesn't work.
// DoThing returns a Thing object, which doesn't have
// DoExtendedThing on it
var extendedThing2 = new ExtendedThing().DoThing().DoExtendedThing()

How could I possibly get DoThing to not return a Thing, but instead return on object of whatever type the calling object has been extended into, even though it would have no idea what that type was at compile time.

Or do I just have to know to call the fluent methods in the "correct" order?

Vlad Stryapko

I believe this can be solved with the following pattern:

class Base<T> where T : Base<T>
{
    public T Thing()
    {
        return (T)this;
    }
}

class Derived : Base<Derived>
{
    public void AnotherThing()
    {
        Console.WriteLine("Hello!");
    }
}

For the line var extended = new Derived().Thing();, extended has the ability to call AnotherThing().

Is it what you're looking for?

Update: as it turns out, if was already answered here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I make external methods interruptable?

From

How can I make two objects comparable

From Java

How can I make the function to wait before calling itself

From Java

How to make array of inherited objects - Java

From

How do I override inherited methods when using JavaScript ES6/ES2015 subclassing

From Dev

Handling null objects when calling methods

From Dev

How can I make each object unique when creating an array of objects using array_fill?

From Dev

How can I make OxyPlot adjust zoom when calling PlotView.Invalidate?

From Dev

How can I access MainActivity's objects or methods inside BroadcastReceiver?

From Dev

How can I make a function complete before calling others in an IBAction?

From Dev

how can I make these methods generic?

From Dev

Membership testing on set of pairs, how do i make the order of element in pairs irrelevant?

From Dev

How can I access a parent method when calling the methods?

From Dev

How can I make order editable linearlayout?

From Dev

How can I create a distinct list of javascript objects, when I need a compound key to make it distinct?

From Dev

How do I (and should I) make additional methods on DOM objects?

From Dev

Makefile:How can I control the partial compilation order when I use 'make -j'?

From Dev

How can I systematically disable certain irrelevant a11y warnings when compiling with Svelte?

From Dev

when i make a $group how can i return the total sum of a property present in all objects?

From Dev

Issuing with calling methods in Inherited class

From Dev

How can I preserve values between WCF service methods calling?

From Dev

How Can I Unit Test Calling of iOS Application Delegate Methods?

From Dev

How can I make T-SQL perform OPTION (FORCE ORDER) when defining views?

From Dev

How can I read .txt files, to inherited objects in c++?

From Dev

How can I handle multiple disposal methods for inherited types?

From Dev

How can I make a list defined in one method available in another method when both methods are in the same class?

From Dev

How can I use case when in order by?

From Dev

How can I order objects in a list?

From Dev

How can I safely handle null values when calling Java methods from Kotlin?

Related Related

  1. 1

    How can I make external methods interruptable?

  2. 2

    How can I make two objects comparable

  3. 3

    How can I make the function to wait before calling itself

  4. 4

    How to make array of inherited objects - Java

  5. 5

    How do I override inherited methods when using JavaScript ES6/ES2015 subclassing

  6. 6

    Handling null objects when calling methods

  7. 7

    How can I make each object unique when creating an array of objects using array_fill?

  8. 8

    How can I make OxyPlot adjust zoom when calling PlotView.Invalidate?

  9. 9

    How can I access MainActivity's objects or methods inside BroadcastReceiver?

  10. 10

    How can I make a function complete before calling others in an IBAction?

  11. 11

    how can I make these methods generic?

  12. 12

    Membership testing on set of pairs, how do i make the order of element in pairs irrelevant?

  13. 13

    How can I access a parent method when calling the methods?

  14. 14

    How can I make order editable linearlayout?

  15. 15

    How can I create a distinct list of javascript objects, when I need a compound key to make it distinct?

  16. 16

    How do I (and should I) make additional methods on DOM objects?

  17. 17

    Makefile:How can I control the partial compilation order when I use 'make -j'?

  18. 18

    How can I systematically disable certain irrelevant a11y warnings when compiling with Svelte?

  19. 19

    when i make a $group how can i return the total sum of a property present in all objects?

  20. 20

    Issuing with calling methods in Inherited class

  21. 21

    How can I preserve values between WCF service methods calling?

  22. 22

    How Can I Unit Test Calling of iOS Application Delegate Methods?

  23. 23

    How can I make T-SQL perform OPTION (FORCE ORDER) when defining views?

  24. 24

    How can I read .txt files, to inherited objects in c++?

  25. 25

    How can I handle multiple disposal methods for inherited types?

  26. 26

    How can I make a list defined in one method available in another method when both methods are in the same class?

  27. 27

    How can I use case when in order by?

  28. 28

    How can I order objects in a list?

  29. 29

    How can I safely handle null values when calling Java methods from Kotlin?

HotTag

Archive