5
I’m trying to make a class using composition, fluent methods and I’m having some problems.
I would like to access a property of a compositional method through fluent interfaces but I cannot implement the abstract method.
The case is as follows:
// Classe base para composição
public abstract class Foo
{
// Propriedade base
public string Param1 { get; set; }
// Método base para retorno da classe (usado com interfaces fluentes)
public abstract Foo GetData();
}
// Classe que compoe Foo
public class Bar : Foo
{
// Propriedade particular de Bar
public string Param2 { get; set; }
// Método para retorno da própria classe (usado com interfaces fluentes)
// Aqui eu não consigo criar o método
public override Bar GetData()
{
return this;
}
}
// Classe que utiliza
public class Test
{
public Test()
{
// Acesso à propriedade particular da classe compositiva
var newBar = new Bar().GetData().Param2;
}
}
Does anyone have any idea how to do that?
I think that the return type of the compositional class method should be the parent object (
public override Foo GetData()
).– gmsantos
You cannot declare this method as override:
public override Bar GetData()
because it does not exist in any class above the hierarchy (does not exist in the parent class). The method in the parent class returnsFoo
and notBar
.– Caffé
The problem of me returning the same object of the father (Foo) in Getdata() is that with it I will not have access to property Param2.
– Jorn Filho