子类化 C# System.Type 的最简单方法
我需要使定点数类继承自System.Type。
class FixedPoint : Type
{
public bool Signed { get; set; }
public int Width { get; set; }
public int IntegerWidth { get; set; }
public FixedPoint(Boolean signed = false, int width = 16, int integerWidth = 8)
{
Signed = signed;
Width = width;
IntegerWidth = integerWidth;
}
}
当我尝试编译此代码时,我收到错误消息,指出我需要实现方法,因为 Type 是抽象类。
userdef.cs(3,7): error CS0534: 'FixedPoint' does not implement inherited abstract member 'System.Type.GUID.get'
userdef.cs(3,7): error CS0534: 'FixedPoint' does not implement inherited abstract member 'System.Type.Namespace.get'
c:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll: (Location of symbol related to previous error)
userdef.cs(3,7): error CS0534: 'FixedPoint' does not implement inherited abstract member
'System.Type.AssemblyQualifiedName.get'
我怎样才能避免这些错误消息?有没有简单的方法来子类化 Type?我必须实现所有方法吗?如果是这样,有这样做的参考吗?
或者
子类化 Type 的工作值得尝试吗?由于某些原因,我确实需要对 Type 进行子类化,如果确实很难做到的话。我还是早点放弃吧,另寻出路。
I need to make fixed point number class inherit from System.Type.
class FixedPoint : Type
{
public bool Signed { get; set; }
public int Width { get; set; }
public int IntegerWidth { get; set; }
public FixedPoint(Boolean signed = false, int width = 16, int integerWidth = 8)
{
Signed = signed;
Width = width;
IntegerWidth = integerWidth;
}
}
When I tried to compile this code, I got error messages saying that I need to implement methods as Type is abstract class.
userdef.cs(3,7): error CS0534: 'FixedPoint' does not implement inherited abstract member 'System.Type.GUID.get'
userdef.cs(3,7): error CS0534: 'FixedPoint' does not implement inherited abstract member 'System.Type.Namespace.get'
c:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll: (Location of symbol related to previous error)
userdef.cs(3,7): error CS0534: 'FixedPoint' does not implement inherited abstract member
'System.Type.AssemblyQualifiedName.get'
How can I avoid those error messages? Is there any easy way to subclass Type? Do I have to implement all the methods? If so, are there any references for doing that?
Or
Is the work of subclassing Type worth trying? I do need to subclass Type for some reasons, if it's really hard to do it. I'd better give up early to find another way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您说您有继承
System.Type
的理由,尽管我同意@mootinator,但以下是您其他问题的一些答案:不。
是的。
您将
override
关键字添加到每个Properties
和Methods
这是如何开始的示例,您需要
覆盖
每个抽象
属性和方法。这是一个完整的可编译类,它覆盖了所有需要的属性和方法,但没有实现任何内容。
这些是您需要实现的属性获取
这些是您需要实现的方法
如您所见,您需要重写很多内容才能消除所有编译错误,因此您要么有充分的理由想要这样做或者你应该考虑从另一个类/结构覆盖或者只是创建一个新的类/结构。
You say you have your reasons for inheriting from
System.Type
, even though I agree with @mootinator, here are some answers to your other questions:No.
Yes.
You add the
override
-keyword to each of theProperties
andMethods
This is an example of how you start off, you need to
override
each of theabstract
properties and methods.This is a complete compileable
class
that overrides all theproperties
andmethods
that is needed, but nothing is implemented.These are the properties gets that you need to implement
These are the methods that you need to implement
As you can see, there are quite a few that you need to override in order to remove all the compilation errors, so either you have a really good reason for wanting to do this or you should think about overriding from another class/struct or just create a new class/struct.
如果您想要创建一个新的值类型,只需使用
struct
而不是class
(不需要子类化Type
)。否则,您希望通过子类化
Type
来完成什么,而您无法使用typeof(TypeName)
来实现?If what you are trying to do is create a new value type, just use
struct
instead ofclass
(no subclassingType
required).Otherwise, what is it you want to accomplish by subclassing
Type
which you can't do withtypeof(TypeName)
?