我需要让定点数类继承自 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;
}
}
当我尝试编译这段代码时,我收到错误消息说我需要实现方法,因为 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'
如何避免这些错误消息?有什么简单的方法可以子类化 Type 吗?我必须实现所有方法吗?如果是这样,是否有这样做的引用资料?
或者
子类化Type的工作值得一试吗?出于某些原因,我确实需要对 Type 进行子类化,如果这真的很难做到的话。我最好早点放弃,另辟蹊径。
最佳答案
你说你有继承 System.Type
的理由,尽管我同意@mootinator,这里有一些对你其他问题的回答:
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?
您将 override
关键字添加到每个 Properties
和 Methods
这是您如何开始的示例,您需要覆盖
每个抽象
属性和方法。
public class Test : Type
{
public override Guid GUID
{
get { throw new NotImplementedException(); }
}
}
这是一个完整的可编译类
,它覆盖了所有需要的属性
和方法
,但没有任何实现。
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
- 基本类型
- 程序集合格名
- 命名空间
- 全名
- 大会
- 模块
- 底层系统类型
- 姓名
这些是您需要实现的方法
- 调用成员
- GetConstructorImpl
- 获取构造函数
- 获取方法实现
- 获取方法
- 获取字段
- 获取事件
- 获取字段
- 获取事件
- 获取接口(interface)
- 获取接口(interface)
- 获取事件
- 获取嵌套类型
- 获取成员(member)
- GetPropertyImpl
- 获取属性
- 获取嵌套类型
- 获取成员(member)
- GetAttributeFlagsImpl
- IsArrayImpl
- IsByRefImpl
- IsPointerImpl
- IsPrimitiveImpl
- IsCOMObjectImpl
- 获取元素类型
- 获取自定义属性
- 有ElementTypeImpl
- 获取自定义属性
- 已定义
如您所见,为了消除所有编译错误,您需要重写很多,所以要么您有充分的理由想要这样做,要么您应该考虑从另一个类重写/结构或只是创建一个新的类/结构。
关于c# - 子类化 C# System.Type 的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6698668/