类型类型 [英] Type of Type
问题描述
Hoi那里,
我是Delphi程序员,几个月前搬到了C#。到目前为止,我对B#非常满意,并且感觉自己很快就舒服了。只需
留下关于类型系统的一些高级问题。
一个问题是类型类型。问题:
在Delphi(Pascal)中,我能够做到以下几点(免费翻译为
C#pseudocode ;-)):
//一个简单的类
公共类MyChildPageBase
{
}
//一个简单的派生类
公共类MyChildTasks:MyChildPageBase
{
}
//基类的指定类型
公共类型MyChildPageBaseclass MyChildPageBaseType;
而不是使用类型I然后可以使用这样的新类型:
public void CreateChildPage(MyChildPageBaseType childType)
{
ChildBase child = new childType.CreateInstance();
这样,编译器确保CreateChildPage()只能使用MyChildPageBase类型或任何派生类调用
。看看
这个:
CreateChildPage(MyChildTasks); //< - 效果很好
CreateChildPage(MyChildPageBase); //< - 效果很好
CreateChildPage(int); //< - 编译错误
我想我可以在C#中使用IS运算符来确保运行时的类型但是
我想查看它在编译时因此我已经得到编译器错误
用于上面代码的最后一行。
有没有人知道我是否以及如何可以用C#做到这一点吗?
干杯,
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
推荐答案
好吧,如果我理解你在做什么,那么在2.0泛型可能是
答案:
public void CreateChildPage< T>( )其中T:MyChildPageBase,new()
{
T child = new T();
//其他什么,也许是回来的孩子与信号。 公共T
创建......
}
然后调用CreateChildPage< MyChildTasks>()或
CreateChildPage< MyChildPageBase>()没问题,但是CreateChildPage< int>()
将在编译时失败。
是这是什么意思?
Marc
Well, if I understand what you are doing, then in 2.0 generics may be the
answer:
public void CreateChildPage<T>() where T : MyChildPageBase, new()
{
T child = new T();
// whatever else, perhaps returning child with the sig. "public T
Create..."
}
Then call CreateChildPage<MyChildTasks>() or
CreateChildPage<MyChildPageBase>() will be fine, but CreateChildPage<int>()
will fail at compile time.
Is this what you mean?
Marc
public void CreateChildPage(Type type)
{
//现在你可以使用IS
如果(类型是MyChildTasks)
返回新类型.CreateInstance();
if(type是MyChildPageBase)
返回new type.CreateInstance();
抛出新的异常(...);
}
但是,我认为像你一样拥有它会更好。作为一个
的程序员,我宁愿在编译时知道如果我传递了一个
不正确的类型,而不是在运行时发现我傻了。
2006年7月5日05:41:07 -0700, ma ** @ idev.ch 写道:
public void CreateChildPage(Type type)
{
// now you can use IS
if(type is MyChildTasks)
return new type.CreateInstance();
if(type is MyChildPageBase)
return new type.CreateInstance();
throw new Exception(...);
}
However, I think it would be better to have it as you did. As a
programmer, I would rather know at compile time if I''m passing an
incorrect type vs. finding out at runtime that I goofed.
On 5 Jul 2006 05:41:07 -0700, ma**@idev.ch wrote:
> Hoi那里,
我是Delphi程序员,几个月前搬到了C#。到目前为止,我对C#非常满意,并且感觉自己很快就很舒服。只剩下关于类型系统的一些高级问题。
一个问题是类型类型。问题:
在Delphi(Pascal)中,我能够做到以下几点(免费翻译为C#pseudocode ;-)):
//一个简单的类
公共课MyChildPageBase
{
}
//一个简单的派生类
公共类MyChildTasks:MyChildPageBase
{
}
//指定类型的基类
公共类型的MyChildPageBaseclass MyChildPageBaseType;
而不是使用Type I然后可以使用这样的新类型:
public void CreateChildPage(MyChildPageBaseType childType)
ChildBase child = new childType.CreateInstance();
}
这个编译器确保只能使用MyChildPageBase类型或任何派生类调用CreateChildPage()。看看
这个:
CreateChildPage(MyChildTasks); //< - 效果很好
CreateChildPage(MyChildPageBase); //< - 效果很好
CreateChildPage(int); //< - 编译错误
我想我可以在C#中使用IS运算符来确保运行时的类型但是
我想在编译时检查它所以我已经得到编译器错误
上面代码的最后一行。
有没有人知道我是否以及如何在C#中做到这一点?
干杯,
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
打开编译器警告;-p
变量type被声明为类型,因此永远不能(继承)
MyChildTasks或MyChildPageBase,因此是这里不是正确的测试;
来替换你的是测试你需要一个(取决于确切的
行为):
if(type == typeof(MyChildTasks))// strict match
或
if(type.IsAssignableFrom(typeof(MyChildTasks)))//确切或子类
如果(type.IsSubclassOf(typeof(MyChildTasks)))//严格的子类
实际创建如下:
type.GetConstructor(Type.EmptyTypes) .Invoke(null);
您的意思是new {concrete class}吗?注意上面的用法也首先删除了测试所需的
,但只在运行时验证。
一切都很乱,是吗?但是,在2.0中,泛型是一个更好的解决方案。参见
上一篇文章。
Marc
Turn on compiler warnings ;-p
The variable "type" is declared as a "Type", so can never be (inherit from)
either MyChildTasks or MyChildPageBase, so "is" is not the right test here;
to replace your "is" tests you would need one of (depending on exact
behaviour wanted):
if(type == typeof(MyChildTasks)) // strict match
OR
if(type.IsAssignableFrom(typeof(MyChildTasks))) // exact or subclass
OR
if(type.IsSubclassOf(typeof(MyChildTasks))) // strict subclass
The actual creation would look like:
type.GetConstructor(Type.EmptyTypes).Invoke(null);
Did you mean "new {concrete class}"? Note the above usage also removes the
need for the tests in the first place, but only validates at runtime.
All very messy, eh? However, in 2.0 generics is a much better solution. See
previous post.
Marc
这篇关于类型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!