要继承C#System.Type的最简单的方法最简单、方法、System、Type

2023-09-06 14:12:55 作者:懒猫吃闲鱼

我需要定点数的类继承System.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;
    }
}

当我试图编译这个code,我得到的错误信息说我需要实现方法为类型是抽象类。

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?

时的子类价值型的工作努力?我确实需要继承类型的一些原因,如果这真的很难做到这一点。我最好放弃早早另谋出路。

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.

推荐答案

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

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:

有没有简单的方法来继承类型?

Is there any easy way to subclass Type?

没有。

我一定要实现所有的方法?

Do I have to implement all the methods?

是的。

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

常用的几款工具让 Kubernetes 集群上的工作更容易

If so, are there any references for doing that?

您的覆盖 -keyword添加到每个属性方法

You add the override-keyword to each of the Properties and Methods

这是你如何开始一个例子,你需要覆盖每个摘要属性和方法

This is an example of how you start off, you need to override each of the abstract properties and methods.

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

这是一个完整的compileable ,它覆盖所有的属性方法这是必要的,但没有实现。

This is a complete compileable class that overrides all the properties and methods that is needed, but nothing is implemented.

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 命名空间 全名 组装 模块 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 IsDefined 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 IsDefined

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

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.

 
精彩推荐
图片推荐