M
marc
Hoi there,
I am a Delphi Programer who moved over to C# some months ago. So far I
am really happy with C# and feelt myself confortable quite fast. Just
some advanced questions regarding the type system are left.
One question is the "type of type" issue:
In Delphi (Pascal) I was able to do the following (free translation to
C# pseudocode ;-) ):
// A simple class
public class MyChildPageBase
{
}
// A simple derived class
public class MyChildTasks : MyChildPageBase
{
}
// A specified type of the base class
public type of MyChildPageBaseclass MyChildPageBaseType;
Instead using Type I then could use this new type like this:
public void CreateChildPage(MyChildPageBaseType childType)
{
ChildBase child = new childType.CreateInstance();
}
This way the compiler ensured that CreateChildPage() could only be
called with the type of MyChildPageBase or any derived class. Look at
this:
CreateChildPage(MyChildTasks); // <- works well
CreateChildPage(MyChildPageBase); // <- works well
CreateChildPage(int); // <- compiler error
I think I can use the IS operator in C# to ensure types at runtime but
I'd like to check it at compile-time so I already get compiler-error
for the last line of the above code.
Does anybody have an idea if and how I can do this in C#?
Cheers,
Marc
I am a Delphi Programer who moved over to C# some months ago. So far I
am really happy with C# and feelt myself confortable quite fast. Just
some advanced questions regarding the type system are left.
One question is the "type of type" issue:
In Delphi (Pascal) I was able to do the following (free translation to
C# pseudocode ;-) ):
// A simple class
public class MyChildPageBase
{
}
// A simple derived class
public class MyChildTasks : MyChildPageBase
{
}
// A specified type of the base class
public type of MyChildPageBaseclass MyChildPageBaseType;
Instead using Type I then could use this new type like this:
public void CreateChildPage(MyChildPageBaseType childType)
{
ChildBase child = new childType.CreateInstance();
}
This way the compiler ensured that CreateChildPage() could only be
called with the type of MyChildPageBase or any derived class. Look at
this:
CreateChildPage(MyChildTasks); // <- works well
CreateChildPage(MyChildPageBase); // <- works well
CreateChildPage(int); // <- compiler error
I think I can use the IS operator in C# to ensure types at runtime but
I'd like to check it at compile-time so I already get compiler-error
for the last line of the above code.
Does anybody have an idea if and how I can do this in C#?
Cheers,
Marc