子类化C#System.Type的最简单方法

pro*_*eek 2 .net c# types subclassing

我需要从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;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译此代码时,我收到错误消息,说我需要实现方法,因为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'
Run Code Online (Sandbox Code Playgroud)

我该如何避免这些错误消息?是否有任何简单的方法来子类型?我必须实施所有方法吗?如果是这样,有没有参考这样做?

要么

子类化Type的工作值得尝试吗?我确实需要将Type子类化为某些原因,如果真的很难这样做的话.我最好早点放弃,找到另一种方式.

Kev*_*ker 5

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

否则,你想要通过子类化来完成什么Type,你不能做typeof(TypeName)什么?


Fil*_*erg 5

你说你有继承的理由System.Type,即使我同意@mootinator,这里有一些你的其他问题的答案:

是否有任何简单的方法来子类型?

没有.

我必须实施所有方法吗?

是.

如果是这样,有没有参考这样做?

你将override-keyword 添加到每个PropertiesMethods

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

public class Test : Type
{
    public override Guid GUID
    {
        get { throw new NotImplementedException(); }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个完整的可编译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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • GUID
  • BASETYPE
  • AssemblyQualifiedName
  • 命名空间
  • 全名
  • 部件
  • UnderlyingSystemType
  • 名称

这些是您需要实现的方法

  • InvokeMember
  • GetConstructorImpl
  • GetConstructors
  • GetMethodImpl
  • 的getMethods
  • getfield命令
  • GetEvent
  • GetFields
  • GetEvents
  • GetInterface
  • GetInterfaces
  • GetEvents
  • GetNestedTypes
  • GetMembers
  • GetPropertyImpl
  • 的GetProperties
  • GetNestedType
  • GetMember
  • GetAttributeFlagsImpl
  • IsArrayImpl
  • IsByRefImpl
  • IsPointerImpl
  • IsPrimitiveImpl
  • IsCOMObjectImpl
  • GetElementType
  • GetCustomAttributes
  • HasElementTypeImpl
  • GetCustomAttributes
  • 被定义为

正如您所看到的,为了消除所有编译错误,您需要覆盖很多,所以要么您有充分的理由想要这样做,要么您应该考虑从另一个类/结构覆盖或只是创建一个新的类/结构.