M
MRe
Hi,
I hope it's OK to post OO questions here[?] I am using C# of
course..
I'm wonder if there's a clean way to do this: I have a library with
a public abstract class, 'PublicClass', that has an internal generic
type, 'T'. 'T' is of no interest to anyone calling on 'PublicClass',
only to inheriting classes. There's also several internal classes,
'InternalClassN', that inherits from 'PublicClass'. What I need is a
way for 'InternalClassN' to tell 'PublicClass' what 'T' is, without
there being a need for the calling code (using 'PublicClass') to know.
Note. 'InternalClassN' is constructed by a factory. E.g., what I'd
like to do in a magical world is (complete example at end of post)..
internal class InternalClassN : PublicClass<SomeTypeN> { .. } //
Library (specify 'T')
PublicClass pc = PublicClassFactory.Construct(..); // Calling code
(doesn't specify 'T')
...is there anything I can do to get this effect?
I know I could just move 'T' into each of 'InternalClassN', but it
seems kind of messy (especially in the more complex code in which I am
actually doing this).
Thank you for any help,
Kind regards,
Eliott
~~~ Complete example ~~~
using System;
namespace HiddenGenerics
{ class Program
{ static void Main(string[] args)
{ PublicClass pc;
pc = PublicClassFactory
.Construct("test");
}
}
public class PublicClassFactory
{ public static
PublicClass Construct
( string Name
){ switch(Name)
{ case "1": return new InternalClass1();
case "2": return new InternalClass2();
}
return null;
}
}
public abstract class PublicClass<T>
{ private T _Value;
}
public abstract class SomeType { }
internal class InternalClass1
: PublicClass<SomeType1> { }
internal class InternalClass2
: PublicClass<SomeType2> { }
internal class SomeType1 : SomeType { }
internal class SomeType2 : SomeType { }
}
I hope it's OK to post OO questions here[?] I am using C# of
course..
I'm wonder if there's a clean way to do this: I have a library with
a public abstract class, 'PublicClass', that has an internal generic
type, 'T'. 'T' is of no interest to anyone calling on 'PublicClass',
only to inheriting classes. There's also several internal classes,
'InternalClassN', that inherits from 'PublicClass'. What I need is a
way for 'InternalClassN' to tell 'PublicClass' what 'T' is, without
there being a need for the calling code (using 'PublicClass') to know.
Note. 'InternalClassN' is constructed by a factory. E.g., what I'd
like to do in a magical world is (complete example at end of post)..
internal class InternalClassN : PublicClass<SomeTypeN> { .. } //
Library (specify 'T')
PublicClass pc = PublicClassFactory.Construct(..); // Calling code
(doesn't specify 'T')
...is there anything I can do to get this effect?
I know I could just move 'T' into each of 'InternalClassN', but it
seems kind of messy (especially in the more complex code in which I am
actually doing this).
Thank you for any help,
Kind regards,
Eliott
~~~ Complete example ~~~
using System;
namespace HiddenGenerics
{ class Program
{ static void Main(string[] args)
{ PublicClass pc;
pc = PublicClassFactory
.Construct("test");
}
}
public class PublicClassFactory
{ public static
PublicClass Construct
( string Name
){ switch(Name)
{ case "1": return new InternalClass1();
case "2": return new InternalClass2();
}
return null;
}
}
public abstract class PublicClass<T>
{ private T _Value;
}
public abstract class SomeType { }
internal class InternalClass1
: PublicClass<SomeType1> { }
internal class InternalClass2
: PublicClass<SomeType2> { }
internal class SomeType1 : SomeType { }
internal class SomeType2 : SomeType { }
}