2011-07-14 18 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

**なぜ**タイプ 'をサブクラス化する必要がありますか?それは意味をなさないので、説明してください。 –

+2

少し野性的に聞こえるが、どうしてSystem.Typeをサブクラス化する必要があるのだろうか? – DEHAAS

+2

現在のリフレクションシステムを拡張しようとしている場合(または独自の実装を実装しようとしている場合のみ)、 'Type'をサブクラス化してください。そうでなければ、新しい' struct'を宣言できます。 – dlev

答えて

5

は、あなたの他の質問にいくつか答えます:

は、タイプをサブクラス化する簡単な方法はありますか?

私はすべてのメソッドを実装する必要がありますか?

はい。

もしそうなら、これを行うための参考資料はありますか?

あなたがPropertiesMethods

これはあなたが始める方法の一例であるのそれぞれにoverride -keywordを追加し、あなたはoverrideabstractプロパティとメソッドのそれぞれに必要です。

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

これは必要とされますが、何も実装されていないすべてのpropertiesmethodsを上書きし、完全なコンパイル可能classです。

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

これらのプロパティが表示されAssemblyQualifiedNameあなたは

  • GUID
  • BaseType
  • を実装する必要があることを取得します
  • 名前空間
  • フルネーム
  • ASSE 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、問題はありません、私はちょうどここで私のホットキーで狂ったコンパイルエラーを1つの画面と他のコードに持っています:)あなたがすべてのメソッドを実装するつもりがない場合は、あなたのコードを使用する他の人が奇妙なエラーに遭遇しないように、たくさんのテストを書いて、それを文書化してください。 –

5

新しい値型を作成する場合は、classの代わりにstructを使用してください(サブクラス化は不要Type必要)。

それ以外の場合は、typeof(TypeName)で行うことができないTypeのサブクラス化によって何を達成したいですか?あなたがここに、私は@mootinatorに同意していても、あなたはSystem.Type継承のためのあなたの理由を持っていると言う

+0

おっと、ありがとうございます@JoelBFant –

関連する問題