2017-08-27 5 views
0

多少テンプレートになるネストされたメンバークラスを持つ汎用クラスを作成しようとしています。私は派生メンバ上でジェネリックスを使用していますが、ジェネリックの使用を許可するためにメインクラスを適切に定義する方法を明確にしていません。汎用クラスベースのメンバーであるネストされた変数の使用方法

私の最初の実装は動作するオブジェクト型でしたが、定義を通してジェネリックを使いたいと思っていました。提供

は、現在の3クラス

構造の階層は、ルックアサイドレコードに似ています。

{イベントテンプレート} ---> {イベントコンテキスト} - > {イベントレコード}

//テンプレート

public class EventTemplate1 
{ 
    public string name { get; set; } 

    // ..... other members 
    public string template1 { get; set; } 
} 

public class EventTemplate2 
{ 
    public string name { get; set; } 
    // ..... other members 
    public string template2 { get; set; } 
} 

//コンテキスト

public class EventData<T> 
{ 

    protected T _context; 
    // ..... other 

    public EventData(T t) 
    { 
     this._context = t; 
    } 

    // ..... other members 

    public T Context 
    { 
     get { return _context; } 
     set { _context = value; } 
    } 

} 

レコードの詳細(オブジェクトあり)

public class writeClass 
{ 

    // ..... other members 
    public object data { get; set; } 

} 

//私はので、私はここにいくつかの重要なをしないのです予想通り、それはかなりの仕事をしません、これを呼び出そうとするとwriteClass

public class writeClass <T> where T : EventData<T> 
{ 
    public writeClass(T t) 
    { 
     data = t; 
    } 
    // ..... other members 
    public EventData<T> data { get; set; } 

} 

にジェネリックをサポートするために変更するオブジェクト

Template1 temp1 = new Template1(); 
     temp1.name = "test 1"; 

     writeClass writeEvent = new writeClass(); 
     writeEvent.data = temp1; 

でコードを呼び出します。

+0

「Template1」とは何ですか?それは 'EventTemplate1'ですか?あなたのEventDataの中にT がEventDataのですので、どこEventDataのでTはEventDataのある場合、あなたはwriteClass から制約を削除 –

+0

....全く 'EventData'を使用していないあなたの呼び出しで、あなたのTはEventDataのですに.. –

答えて

0

問題

public class writeClass <T> where T : EventData<T> 

はあなたのEventDataの

public class BaseEventTemplate { 
     public string name { get; set; } 
    } 

public class EventTemplate1 : BaseEventTemplate { 
    // ..... other members 
    public string template1 { get; set; } 
} 

public class EventTemplate2: BaseEventTemplate { 
    // ..... other members 
    public string template2 { get; set; } 
} 

public class EventData<T> where T : BaseEventTemplate { 
    protected T _context; 
// ..... other 

public EventData(T t) 
{ 
    this._context = t; 
} 

// ..... other members 

public T Context 
{ 
    get { return _context; } 
    set { _context = value; } 
} 
ためBaseClassのを確認し

public class writeClass <EventData<EventData<EventData<EventData<EventData....>>>> where T : EventData<T> 

ソリューション

に解決します

}

を変更し、次の

public class writeClass <T> where T : EventData<BaseEventTemplate> { 
    public writeClass(T t) 
    { 
     data = t; 
    } 
    // ..... other members 
    public EventData<BaseEventTemplate> data { get; set; } 

} 

のようなあなたのwriteClassにおける制約が

Template1 temp1 = new Template1(); 
    temp1.name = "test 1"; 

    writeClass writeEvent = new writeClass(temp1); 
    Foo foo = writeEvent.data; 

オブジェクトを呼び出す対処方法2

すべての派生のためwriteClassの特定の実装を行いますタイプを作成するには、インタフェースを作成しますのwriteClass

public interface IWriteClass<T> : where T : EventData<BaseEventTemplate> 

public class WriteTemplate1 : IWriteClass<EventData<Template1> 

public class WriteTemplate2 : IWriteClass<EventData<Template2> 

解決策2は、多かれ少なかれSolution1に追加されています。しかし、インターフェイスを使用しない場合は、writeClassクラスのパラメータの型をチェックし、それを特定の型にキャストする必要があります。

関連する問題