2016-06-30 6 views
0

私はIFooBarインターフェイスを実装するFooとBarクラスを持っています。他のクラスのBaseから継承しているDerivedFooとDerivedBarの2つのクラスがあります。 IFooBarには、Baseをパラメータとして使用するメソッドがあります。C#でのポリモーフィズムと関連関係

私はFooとBarにタイプに応じてBaseまたはDerivedFoo/DerivedBarを使用したいと思います。私は適切にインターフェイスを実装していないので、この実装は、あなたが期待通りに、失敗した

public abstract class Base : SuperBase 
{ 

} 

public class DerivedFoo : Base 
{ 
    public string FooProperty {get; set;} 
} 

public class DerivedBar : Base 
{ 
    public int FooProperty {get; set;} 
} 


public interface IFooBar 
{ 
    Base Update(Base toUpdate); 
} 

public class Foo : IFooBar 
{ 
    public Base Update(DerivedFoo toUpdate) 
    { 
    toUpdate.FooProperty = "X"; 
    return toUpdate; 
    } 
} 



public class Bar: IFooBar 
    { 
     public Base Update(DerivedBar toUpdate) 
     { 
     toUpdate.BarProperty = 42; 
     return toUpdate; 
     } 
    } 

:あなたより多くの視覚exemple(C#)を与えること

。私はそれを適切に実装する(DerivedFoo型をBase型に置き換える)私のプロパティFooProperty/BarPropertyを見つけることができません。私が求める行動を達成するためのヒントはありますか?

EDIT:Aleksey L.ソリューションを実装すると、IFooBarのリストが必要です。ObservableCollectionを使用します。

以前、私は(他のクラスで)やっていた(ジェネリックの使用前):

コンストラクタで
private ObservableCollection<IFooBar> m_foobarlist; 

     public ObservableCollection<IFooBar> FooBarList 
     { 
      get 
      { 
       return m_foobarlist; 
      } 
      set 
      { 
       m_foobarlist= value; 
       // other work 
      } 
     } 

:私はアレクセイのヒントの後に変更されたクラスを使用しようとした

FooBarList = new ObservableCollection<IFooBar> 
      { 
       new Foo(), 
       new Bar() 
      }; 

プライベートObservableCollection> m_foobarlist;コンストラクタで

 public ObservableCollection<IFooBar<Base>> FooBarList 
     { 
      get 
      { 
       return m_foobarlist; 
      } 
      set 
      { 
       m_foobarlist= value; 
       // other work 
      } 
     } 

FooBarList = new ObservableCollection<IFooBar<Base>> 
      { 
       new Foo(), 
       new Bar() 
      }; 

しかし、これは

答えて

3

使用generics( 'IFooBarにバーを変換できません' 'Footo IFooBarを変換できません')動作していないよう:

public interface IFooBar<T> where T : Base 
{ 
    T Update(T toUpdate); 
} 

public class Foo : IFooBar<DerivedFoo> 
{ 
    public DerivedFoo Update(DerivedFoo toUpdate) 
    { 
     toUpdate.FooProperty = "X"; 
     return toUpdate; 
    } 
} 

public class Bar : IFooBar<DerivedBar> 
{ 
    public DerivedBar Update(DerivedBar toUpdate) 
    { 
     toUpdate.BarProperty = 42; 
     return toUpdate; 
    } 
} 

更新。わからない何を達成しようとしているが、IFooBarのリストを持っているために、あなたは、このインターフェイス上で基本クラスを定義することができます。

public interface IFooBar 
{ 
    Base Update(Base toUpdate); 
} 

public abstract class BaseFooBar<T> : IFooBar where T : Base 
{ 
    protected abstract T UpdateDerived(T Base); 

    public Base Update(Base toUpdate) 
    { 
     var derived = toUpdate as T; 
     if (derived == null) 
     { 
      //not expected type. decide what you want to do in this case. throw exception? 
     } 

     return UpdateDerived(derived); 
    } 
} 

public class Foo : BaseFooBar<DerivedFoo> 
{ 
    protected override DerivedFoo UpdateDerived(DerivedFoo toUpdate) 
    { 
... 
+0

編集:この戦略でIFooBarのObservableCollectionをどのように宣言して初期化しますか? – Csi

+0

ユースケースについて詳しく説明できますか? –

+0

元の投稿を編集しましたが、それがはっきりしていないかどうか、またはあなたが期待したものがないかどうか教えてください。 – Csi

0

アレクセイ方法は良いようですが、私はこのトリックを使用して終了:

public class Foo : IFooBar 
{ 
    public Base Update(Base toUpdate) 
    { 
    var fooInstance = toUpdate as Foo; 
    if(fooInstance == null) 
    { 
     return null; 
    } 
    fooInstance.FooProperty = "X"; 
    return fooInstance ; 
    } 
} 
関連する問題