创建c# System.Type子类的最简单方法

本文关键字:最简单 方法 子类 Type System 创建 | 更新日期: 2023-09-27 18:02:02

我需要从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;
    }
}

当我试图编译这段代码时,我得到的错误消息说,我需要实现的方法类型是抽象类。

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,如果这真的很难做到的话。我最好早点放弃,另找办法。

创建c# System.Type子类的最简单方法

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

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

我必须实现所有的方法吗?

是的。

如果有,有什么参考资料吗?

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
  • BaseType
  • AssemblyQualifiedName
  • 名称空间
  • FullName
  • <
  • 装配/gh><
  • 模块/gh>
  • UnderlyingSystemType
  • <
  • 名称/gh>

这些是你需要实现的方法

  • InvokeMember
  • GetConstructorImpl
  • GetConstructors
  • GetMethodImpl
  • GetMethods
  • GetField
  • GetEvent
  • GetFields
  • GetEvents
  • GetInterface
  • getinterface
  • GetEvents
  • GetNestedTypes
  • GetMembers
  • GetPropertyImpl
  • getproperty
  • GetNestedType
  • GetMember
  • GetAttributeFlagsImpl
  • IsArrayImpl
  • IsByRefImpl
  • IsPointerImpl
  • IsPrimitiveImpl
  • IsCOMObjectImpl
  • GetElementType
  • GetCustomAttributes
  • HasElementTypeImpl
  • GetCustomAttributes
  • IsDefined

正如你所看到的,有相当多的你需要重写,以消除所有的编译错误,所以你有一个很好的理由想要这样做,或者你应该考虑从另一个类/结构重写,或者只是创建一个新的类/结构。

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

否则,你想通过子类化Type来完成typeof(TypeName)做不到的事情吗?