Call an override method in an abstract class
i have a problem. I try to start the override Host method from Program.cs
in the abstract class AbstractGenericClass.
public abstract class AbstractGenericClass<T>
{
protected abstract void Host();
public static void Start()
{
//add additional logic for all classes that use this
try
{
((AbstractGenericClass<T>)
Activator.CreateInstance(typeof(T))).Host();
Console.WriteLine("Works!");
}
catch (Exception ex)
{
Console.WriteLine("Don't Works!");
}
}
}
class AnotherClass
{
public void DoSomething()
{
//NOP
}
}
class Program
: AbstractGenericClass<AnotherClass>
{
static void Main(string[] args)
{
Program.Start();
Console.ReadLine();
}
protected override void Host()
{
Console.WriteLine("Host running...");
}
}
I add here all sample classes i create for showing what i mean. The line
with ((AbstractGenericClass) Activator.CreateInstance(typeof(T))).Host();
crash the program because of InvalidCastException. It must be possible to
call the Host method but i have no idea how i could this, if this dont
operates.
Have you any other idea how this could operate? Or is this totally wrong
what i try?
No comments:
Post a Comment