Question

i have interface:

public interface Inx<T>
{
   T Set(Data data);
}

simple class with this metod

public class Base
{
   ??? Set(Data data) { ... }
}

and parent class like that:

public class Parent : Base, Inx<Parent>
{
   ...
}

i want to return Parent type from Set metod in child class It's possible ? I need it do to something like that:

list.Add(new Parent().Set(data));

Now i have to do that:

T t = new T();
t.Set(data);
list.Add(t);

And its little annoying, i have to use it many time


Sorry for spaming well hmm i can use something like that:

this.GetType().GetConstructor(new System.Type[] { typeof(Data) }).Invoke(new object[] { data })

so maybe good soluction is return a object from this method ;\ ?

well generic class with generic interface seem's be big memory waste ... beacose function of this class are same only return type is diffrent

Was it helpful?

Solution

Is this what you want?

interface Inx<T> { 
    T Set(Data data);
}
public class Base
{
    public virtual T Set<T>(Data data)
    {
        T t = default(T);
        return t;
    }
}
public class Parent : Base, Inx<Parent>
{
    public Parent Set(Data data)
    {
        return base.Set<Parent>(data);
    }
}
class Program
{
    static void Main(string[] args)
    {
        var data = new Data();
        var list = new List<Parent>();
        list.Add(new Parent().Set<Parent>(data));
        // or 
        list.Add(new Parent().Set(data));
    }
}

EDIT: It's better to move the interface implementation up to the Base class as Marc said:

interface Inx<T> { 
    T Set(Data data);
}
public class Base<T> : Inx<T>
{
    public virtual T Set(Data data)
    {
        T t = default(T);
        return t;
    }
}
public class Parent : Base<Parent>
{        
}
class Program
{
    static void Main(string[] args)
    {
        var data = new Data();
        var list = new List<Parent>();
        list.Add(new Parent().Set(data));            
    }
}

OTHER TIPS

The only way you could do that would be to make the Base generic (Base<T>) and have Set return T - then have Parent : Base<Parent>. The problem is... how would Set know how to create the T? You could have the where T : new() clause...

A useful aside here is that you can move the interface implementation into Base<T>:

public class Base<T> : Inx<T> where T : new()
{
   public T Set(Data data) {
       T t = new T();
       ///
       return t;
   }
}
public class Parent : Base<Parent> { }

well if i use generic base Class i can't cast parent class to one type, i don't know why i tried use generic there ... i found solution now :)

thx for help

interface Inx
{
   object Set(Data data);
}
class Base
{
   object Set(Data data)
   {
      // ...
      return (object) this;
   }
}
class Parent: Base, Inx
{
   // i don't have too write here Set (im using from base one)
}
class ManageParent<T> where T: Inx, new()
{
   // ...
      list.Add((T) new T().Set(data));
   // ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top