A
Alphapage
Hello again,
My last question which opened a big debate about casting such a generic
interface learned me a lot, but I can't find a way because of my particular
generic interface.
I will shown you my code, but when you will answer my post, don't write:
- he is stupid (I already know)
- this is not Object Oriented
- generic is not made for that ...
abstract class BaseStaticOverride<T> where T : BaseStaticOverride<T>
{
private static T _instance;
private static object _lock = new object();
public static T Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
_instance =
(T)Activator.CreateInstance(typeof(T), true);
}
}
return _instance;
}
}
}
abstract class C1Base<T> : BaseStaticOverride<T> where T : C1Base<T>
{
protected C1Base()
{
}
public void WriteLine()
{
Console.WriteLine(Line);
}
protected abstract string Line
{
get;
}
}
class C1 : C1Base<C1>
{
protected C1()
{
}
protected override string Line
{
get { return "C1"; }
}
}
C1 can be considered as a static method with my code and so C1 and
C1Base<C1> are the same class.
The problem occures when I don't know which class implement C1Base
(C1Base<?>). Since C1Base<?> can be considered as static, I think it would be
possible to do this:
C1Base<?>.Instance.WriteLine(); // which will return "C1"
Hope you understand what I want to do.
Thanks in advance for your help.
My last question which opened a big debate about casting such a generic
interface learned me a lot, but I can't find a way because of my particular
generic interface.
I will shown you my code, but when you will answer my post, don't write:
- he is stupid (I already know)
- this is not Object Oriented
- generic is not made for that ...
abstract class BaseStaticOverride<T> where T : BaseStaticOverride<T>
{
private static T _instance;
private static object _lock = new object();
public static T Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
_instance =
(T)Activator.CreateInstance(typeof(T), true);
}
}
return _instance;
}
}
}
abstract class C1Base<T> : BaseStaticOverride<T> where T : C1Base<T>
{
protected C1Base()
{
}
public void WriteLine()
{
Console.WriteLine(Line);
}
protected abstract string Line
{
get;
}
}
class C1 : C1Base<C1>
{
protected C1()
{
}
protected override string Line
{
get { return "C1"; }
}
}
C1 can be considered as a static method with my code and so C1 and
C1Base<C1> are the same class.
The problem occures when I don't know which class implement C1Base
(C1Base<?>). Since C1Base<?> can be considered as static, I think it would be
possible to do this:
C1Base<?>.Instance.WriteLine(); // which will return "C1"
Hope you understand what I want to do.
Thanks in advance for your help.