I
Israel
Can someone explain to me why I get a compiler error with the
following code on the line obj.DoSomething()?
but if cast the object I get no error. The error I get is:
Cannot access protected member
'GenericDerivedClassTest.Base.DoSomething()' via a qualifier of type
'GenericDerivedClassTest.Base'; the qualifier must be of type
'GenericDerivedClassTest.TBase<DerivedType>' (or derived from it)
Why does it believe the object has to derive from TBase<DerivedType>?
when all it needs to do is derive from Base since that's where the
method is and that's also what TBase<DerivedType> derives from.
It seems to me that the compiler is confused because generics are kind
of like templates/macros and maybe doesn't know how to really traverse
the inheritance hierarchy in all cases?
here's the code:
/// <summary>
/// Templated absract class
/// </summary>
/// <typeparam name="DerivedType"></typeparam>
public abstract class TBase<DerivedType> : Base
where DerivedType : TBase<DerivedType>, new()
{
public TBase()
{
}
public static DerivedType StaticDoSomething()
{
// Create a new derived class type
Base obj = (Base)(new DerivedType());
// Let the base class first do something to the object
StaticDoSomethingImpl(ref obj);
// Call the derived class
#if true // This yields the error "Cannot access protected
member..."
obj.DoSomething();
#else // This works fine
TBase<DerivedType> obj2 = (TBase<DerivedType>)obj;
obj2.DoSomething();
#endif
return (DerivedType)obj;
}
}
/// <summary>
/// Abstract base class
/// </summary>
public abstract class Base
{
public Base()
{
}
public static void StaticDoSomethingImpl(ref Base obj)
{
// Do something to obj
}
protected abstract void DoSomething();
}
following code on the line obj.DoSomething()?
but if cast the object I get no error. The error I get is:
Cannot access protected member
'GenericDerivedClassTest.Base.DoSomething()' via a qualifier of type
'GenericDerivedClassTest.Base'; the qualifier must be of type
'GenericDerivedClassTest.TBase<DerivedType>' (or derived from it)
Why does it believe the object has to derive from TBase<DerivedType>?
when all it needs to do is derive from Base since that's where the
method is and that's also what TBase<DerivedType> derives from.
It seems to me that the compiler is confused because generics are kind
of like templates/macros and maybe doesn't know how to really traverse
the inheritance hierarchy in all cases?
here's the code:
/// <summary>
/// Templated absract class
/// </summary>
/// <typeparam name="DerivedType"></typeparam>
public abstract class TBase<DerivedType> : Base
where DerivedType : TBase<DerivedType>, new()
{
public TBase()
{
}
public static DerivedType StaticDoSomething()
{
// Create a new derived class type
Base obj = (Base)(new DerivedType());
// Let the base class first do something to the object
StaticDoSomethingImpl(ref obj);
// Call the derived class
#if true // This yields the error "Cannot access protected
member..."
obj.DoSomething();
#else // This works fine
TBase<DerivedType> obj2 = (TBase<DerivedType>)obj;
obj2.DoSomething();
#endif
return (DerivedType)obj;
}
}
/// <summary>
/// Abstract base class
/// </summary>
public abstract class Base
{
public Base()
{
}
public static void StaticDoSomethingImpl(ref Base obj)
{
// Do something to obj
}
protected abstract void DoSomething();
}