The easiest way to subclassing C# System.Type

吃可爱长大的小学妹 提交于 2019-12-24 05:39:07

问题


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.


回答1:


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?

No.

Do I have to implement all the methods?

Yes.

If so, are there any references for doing that?

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(); }
    }
}

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();
    }
}

These are the properties gets that you need to implement

  • GUID
  • BaseType
  • AssemblyQualifiedName
  • Namespace
  • FullName
  • Assembly
  • Module
  • UnderlyingSystemType
  • Name

These are the methods that you need to implement

  • 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.




回答2:


If what you are trying to do is create a new value type, just use struct instead of class (no subclassing Type required).

Otherwise, what is it you want to accomplish by subclassing Type which you can't do with typeof(TypeName)?



来源:https://stackoverflow.com/questions/6698668/the-easiest-way-to-subclassing-c-sharp-system-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!