【问题标题】:The easiest way to subclassing C# System.Type继承 C# System.Type 的最简单方法
【发布时间】:2011-07-14 19:11:04
【问题描述】:

我需要让定点数类继承自 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 子类化,如果真的很难做到的话。我还是早点放弃吧。

【问题讨论】:

  • 为什么你需要继承Type?请解释一下,因为它没有意义。
  • 听起来有点狂野,为什么要继承 System.Type?
  • 只有当你试图扩展当前的反射系统(或者实现你自己的,我想)你才应该继承Type。否则,你可以声明一个新的struct,没有继承必填。
  • 看起来你正在尝试做类似en.wikipedia.org/wiki/Dependent_type的事情

标签: c# .net types subclassing


【解决方案1】:

您说您有理由从System.Type 继承,即使我同意@mootinator,以下是您其他问题的一些答案:

有没有简单的方法来继承 Type?

没有。

我必须实现所有方法吗?

是的。

如果有,是否有任何参考资料?

您将override-关键字添加到每个PropertiesMethods

这是一个如何开始的示例,您需要 override 每个 abstract 属性和方法。

public class Test : Type
{
    public override Guid GUID
    {
        get { throw new NotImplementedException(); }
    }
}

这是一个完全可编译的class,它覆盖了所有需要的propertiesmethods,但没有实现任何东西。

public class Test : Type
{
    public override Guid GUID
    {
        get { throw new NotImplementedException(); }
    }

    public override bool IsDefined(Type attributeType, bool inherit)
    {
        throw new NotImplementedException();
    }
    public override object[] GetCustomAttributes(bool inherit)
    {
        throw new NotImplementedException();
    }
    public override string Name
    {
        get { throw new NotImplementedException(); }
    }
    protected override bool HasElementTypeImpl()
    {
        throw new NotImplementedException();
    }
    public override object[] 
           GetCustomAttributes(Type attributeType, bool inherit)
    {
        throw new NotImplementedException();
    }
    public override Type UnderlyingSystemType
    {
        get { throw new NotImplementedException(); }
    }
    public override Type GetElementType()
    {
        throw new NotImplementedException();
    }
    protected override bool IsCOMObjectImpl()
    {
        throw new NotImplementedException();
    }
    protected override bool IsPrimitiveImpl()
    {
        throw new NotImplementedException();
    }
    protected override bool IsPointerImpl()
    {
        throw new NotImplementedException();
    }
    protected override bool IsByRefImpl()
    {
        throw new NotImplementedException();
    }
    protected override bool IsArrayImpl()
    {
        throw new NotImplementedException();
    }
    protected override System.Reflection.TypeAttributes 
                       GetAttributeFlagsImpl()
    {
        throw new NotImplementedException();
    }
    public override System.Reflection.MemberInfo[] 
           GetMember(string name, System.Reflection.BindingFlags bindingAttr)
    {
        return base.GetMember(name, bindingAttr);
    }
    public override Type 
           GetNestedType(string name, System.Reflection.BindingFlags bindingAttr)
    {
        throw new NotImplementedException();
    }
    public override System.Reflection.PropertyInfo[] 
           GetProperties(System.Reflection.BindingFlags bindingAttr)
    {
        throw new NotImplementedException();
    }
    protected override System.Reflection.PropertyInfo 
              GetPropertyImpl(string name, System.Reflection.BindingFlags bindingAttr, 
                              System.Reflection.Binder binder, Type returnType, Type[] types, 
                              System.Reflection.ParameterModifier[] modifiers)
    {
        throw new NotImplementedException();
    }
    public override System.Reflection.MemberInfo[] 
           GetMembers(System.Reflection.BindingFlags bindingAttr)
    {
        throw new NotImplementedException();
    }
    public override Type[] GetNestedTypes(System.Reflection.BindingFlags bindingAttr)
    {
        throw new NotImplementedException();
    }
    public override System.Reflection.EventInfo[] GetEvents()
    {
        return base.GetEvents();
    }
    public override Type[] GetInterfaces()
    {
        throw new NotImplementedException();
    }
    public override Type GetInterface(string name, bool ignoreCase)
    {
        throw new NotImplementedException();
    }
    public override System.Reflection.EventInfo[] 
           GetEvents(System.Reflection.BindingFlags bindingAttr)
    {
        throw new NotImplementedException();
    }
    public override System.Reflection.FieldInfo[] 
           GetFields(System.Reflection.BindingFlags bindingAttr)
    {
        throw new NotImplementedException();
    }
    public override System.Reflection.EventInfo 
           GetEvent(string name, System.Reflection.BindingFlags bindingAttr)
    {
        throw new NotImplementedException();
    }
    public override System.Reflection.FieldInfo 
           GetField(string name, System.Reflection.BindingFlags bindingAttr)
    {
        throw new NotImplementedException();
    }
    public override System.Reflection.MethodInfo[] 
           GetMethods(System.Reflection.BindingFlags bindingAttr)
    {
        throw new NotImplementedException();
    }
    protected override System.Reflection.MethodInfo 
              GetMethodImpl(string name, System.Reflection.BindingFlags bindingAttr,
                            System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention, 
                            Type[] types, System.Reflection.ParameterModifier[] modifiers)
    {
        throw new NotImplementedException();
    }
    public override System.Reflection.ConstructorInfo[] GetConstructors(System.Reflection.BindingFlags bindingAttr)
    {
        throw new NotImplementedException();
    }
    protected override System.Reflection.ConstructorInfo 
              GetConstructorImpl(System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder,
                                 System.Reflection.CallingConventions callConvention, Type[] types, 
                                 System.Reflection.ParameterModifier[] modifiers)
    {
        throw new NotImplementedException();
    }
    public override Type BaseType
    {
        get { throw new NotImplementedException(); }
    }
    public override string AssemblyQualifiedName
    {
        get { throw new NotImplementedException(); }
    }
    public override string Namespace
    {
        get { throw new NotImplementedException(); }
    }
    public override string FullName
    {
        get { throw new NotImplementedException(); }
    }
    public override System.Reflection.Assembly Assembly
    {
        get { throw new NotImplementedException(); }
    }
    public override System.Reflection.Module Module
    {
        get { throw new NotImplementedException(); }
    }
    public override object 
           InvokeMember(string name, System.Reflection.BindingFlags invokeAttr, 
                        System.Reflection.Binder binder, object target, object[] args, 
                        System.Reflection.ParameterModifier[] modifiers, 
                        System.Globalization.CultureInfo culture, string[] namedParameters)
    {
        throw new NotImplementedException();
    }
}

这些是您需要实现的属性获取

  • GUID
  • 基本类型
  • AssemblyQualifiedName
  • 命名空间
  • 全名
  • 组装
  • 模块
  • 基础系统类型
  • 姓名

这些是您需要实现的方法

  • InvokeMember
  • GetConstructorImpl
  • 获取构造函数
  • GetMethodImpl
  • GetMethods
  • 获取字段
  • 获取事件
  • 获取字段
  • 获取事件
  • 获取接口
  • 获取接口
  • 获取事件
  • GetNestedTypes
  • 获取会员
  • GetPropertyImpl
  • 获取属性
  • GetNestedType
  • 获取会员
  • GetAttributeFlagsImpl
  • IsArrayImpl
  • IsByRefImpl
  • IsPointerImpl
  • IsPrimitiveImpl
  • IsCOMObjectImpl
  • 获取元素类型
  • GetCustomAttributes
  • HasElementTypeImpl
  • GetCustomAttributes
  • 已定义

如您所见,为了消除所有编译错误,您需要重写很多内容,所以要么您有充分的理由想要这样做,要么您应该考虑从另一个类/结构或只是创建一个新的类/结构。

【讨论】:

  • @prosseek,没问题,我只是用我的热键发疯了,在一个屏幕上显示编译错误,在另一个屏幕上显示代码:) 只是指出一些事情,如果你不打算要实现所有方法,请确保编写大量测试并将其记录在案,以便其他使用您的代码的人不会遇到奇怪的错误。
【解决方案2】:

如果您要创建一个新的值类型,只需使用struct 而不是class(不需要子类化Type)。

否则,您想通过子类化 Type 来实现什么,而您无法使用 typeof(TypeName) 来实现?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2015-06-05
    相关资源
    最近更新 更多