R
Richard Friend
Does anyone know how to get round this problem?
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary1
{
public class BaseClass
{
public int counter = 0;
}
public class GenericClass<T> : BaseClass where T : BaseClass,new()
{
public T IncrementCounterAndCopy()
{
T t = new T();
t.counter = this.counter+1;
return t;
}
}
public class TestClass1 : GenericClass<TestClass1>
{
}
public class TestClass2 : TestClass1
{
}
public class TestIt
{
public void x()
{
//works ok
TestClass1 t1 = new TestClass1();
TestClass1 t2 = t1.IncrementCounterAndCopy();
TestClass2 tt1 = new TestClass2();
//How can i ensure the return type is of the actual type of my
class
//WITHOUT making all classes in the chain also generic...?
//Possible??
TestClass2 tt2 = t1.IncrementCounterAndCopy(); //compiler error
here...
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary1
{
public class BaseClass
{
public int counter = 0;
}
public class GenericClass<T> : BaseClass where T : BaseClass,new()
{
public T IncrementCounterAndCopy()
{
T t = new T();
t.counter = this.counter+1;
return t;
}
}
public class TestClass1 : GenericClass<TestClass1>
{
}
public class TestClass2 : TestClass1
{
}
public class TestIt
{
public void x()
{
//works ok
TestClass1 t1 = new TestClass1();
TestClass1 t2 = t1.IncrementCounterAndCopy();
TestClass2 tt1 = new TestClass2();
//How can i ensure the return type is of the actual type of my
class
//WITHOUT making all classes in the chain also generic...?
//Possible??
TestClass2 tt2 = t1.IncrementCounterAndCopy(); //compiler error
here...
}
}
}