views:

821

answers:

5

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

+4  A: 

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> { }
Marc Gravell
A: 

REPLY:

well, all methods from this base class can read all fields in parent class, if i make class

Parent : Child
{
   int aa;
   int bb;
}

and use

Child.Set() i this method can modify/use aa,bb value, and i can get parent type:

Type t = this.GetType();
foreach (FieldInfo f in t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance))

but propably You have right, i can't know this type before compilation :\ so i should use then: class Parent : Base<Parent>, Inx<Parent> ... is no other solution ?

thx for help anyway :)

kaem
Reflection (especially against fields) is rarely a good idea... what is the specific scenario?
Marc Gravell
A: 

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

kaem
No that's not good. A generic class with a generic interface is not a big memory waste. One of the major reasons for generics is to prevent that kind of thing you are suggesting here, and you want to use generics in the case where the types vary like this.
Richard Hein
A: 

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));            
    }
}
Richard Hein
A: 

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));
   // ...
}
kaem