2011-07-14 83 views
2

我需要从System.Type继承定点数字类。继承C#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?我必须实施所有的方法吗?如果是这样,那么有没有任何参考资料呢?

是否值得尝试的子类型工作?如果真的很难做到这一点,我确实需要将类型进行子类化。我最好早点放弃寻找另一种方式。

+5

**为什么**你需要子类'Type'?请解释一下,因为这没有意义。 –

+2

听起来有点狂野,为什么你需要继承System.Type? – DEHAAS

+2

只有当你试图扩展当前的反射系统(或者实现你自己的,我想)时,你应该继承'Type'。否则,你可以声明一个新的'struct',不需要继承。 – dlev

回答

5

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

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

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

是的。

如果是这样,那么有没有任何参考文献呢?

您添加override -keyword到每个PropertiesMethods

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

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

这是一个完整的可编译class,它覆盖所有properties,同时还需要methods,但没有实现。

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
  • 命名空间
  • 全名
  • 奥丝mbly
  • 模块
  • UnderlyingSystemType
  • 名称

这些都是你需要实现

  • InvokeMember
  • GetConstructorImpl方法
  • GetConstructors
  • GetMethodImpl
  • 的getMethods
  • getfield命令
  • GetEvent
  • GetFields
  • GetEvents
  • GetInterface
  • GetInterfaces
  • GetEvents
  • GetNestedTypes
  • 个GetMembers
  • GetPropertyImpl
  • 的GetProperties
  • GetNestedType
  • GetMember
  • GetAttributeFlagsImpl
  • IsArrayImpl
  • IsByRefImpl
  • IsPointerImpl
  • IsPrimitiveImpl
  • ISCO MObjectImpl
  • GetElementType
  • GetCustomAttributes
  • HasElementTypeImpl
  • GetCustomAttributes
  • IsDefined

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

+0

非常感谢!我只是可以绕过编译器错误。 – prosseek

+0

@prosseek,没问题,我只是疯狂地用我的热键,在一个屏幕上出现编译错误,另一个屏幕上出现代码:)只是指出一些事情,如果你不打算实现所有的方法,确保你编写了很多测试并记录下来,以便其他人使用你的代码不会遇到奇怪的错误。 –

5

如果您正在尝试创建新的值类型,则只需使用struct而不是class(不需要子类Type即可)。

否则,您想通过继承Type来完成哪些工作?typeof(TypeName)

+0

哎呀,谢谢@JoelBFant –