2017-09-29 5 views
0

ObservableCollection to thaatメソッドを渡す必要があるC#のメソッドがあります。クラスTでプロパティとその値を取得する必要があります。ただし、クラスTには任意のクラス名を使用できます。任意のタイプのクラスのobservablecollectionを渡し、メソッド内のプロパティを取得する - C#

例えば:

public class MyClass 
{ 
    public string prop1 {get; set;} 
    public prop2 {get; set; } 
} 

public class OtherClass 
{ 
    public string OtherProp1 {get; set;} 
    public OtherProp2 {get; set;} 
} 

private ObservableCollection<MyClass> _myselectedItems = new ObservableCollection<MyClass>(); 

public ObservableCollection<MyClass> MySelectedItems 
{ 
    get{return _myselectedItem;} 
    set{_myselectedItem = value;} 
} 

private ObservableCollection<OtherClass> _otherselectedItems = new ObservableCollection<OtherClass>(); 

public ObservableCollection<OtherClass> OtherSelectedItems 
{ 
    get{return _otherselectedItem;} 
    set{_otherselectedItem = value;} 
} 

public GenericMethod<T>(ObservableCollection<T> anySelectedItems) 
{ 
    if (anySelectedItems[0].**prop1** != "Hello") 
    { // do something 
    } 
} 

私は呼びたい、この一般的な方法があります。

---->ここでは、このメソッドが呼び出される場所とanySelectedItemsの型に基づいて、クラスの対応するプロパティ(prop1、prop2、otherProp1、またはOtherProp2)にアクセスできるようにしたいと考えています。合格値がObservableCollection<MyClass>なら、私はprop1とprop2を取得する必要があります。

ご迷惑をおかけして申し訳ございません。おかげさまで

+0

[ジェネリッククラスのプロパティを取得]の可能な複製(https://stackoverflow.com/questions/14129421/get-property-of-generic-class) –

+0

******注:最後の文の訂正:渡された値がObservableCollection の場合、私はprop1とprop2を取得する必要があります。 – Always23

+0

@ Kitty23コメントに間違いはありません。投稿を編集してください。 – maccettura

答えて

0

私のコメントによると、次のようにapporachをお勧めします。インターフェイスはあなたのために仕事をしましょう。

public interface IMyClass { 
    string prop1 {get; set;} 
    string prop2 {get; set;} 
} 

public class MyClass : IMyClass 
{ 
    public string prop1 {get; set;} 
    public string prop2 {get; set; } 
} 

public class OtherClass : IMyClass 
{ 
    public string prop1 {get; set;} 
    public string prop2 {get; set;} 
} 

private ObservableCollection<MyClass> _myselectedItems = new ObservableCollection<MyClass>(); 

public ObservableCollection<IMyClass> MySelectedItems 
{ 
    get{return _myselectedItem;} 
    set{_myselectedItem = value;} 
} 

private ObservableCollection<OtherClass> _otherselectedItems = new ObservableCollection<OtherClass>(); 

public ObservableCollection<OtherClass> OtherSelectedItems 
{ 
    get{return _otherselectedItem;} 
    set{_otherselectedItem = value;} 
} 

public GenericMethod(ObservableCollection<IMyClass> anySelectedItems) 
{ 
    if (anySelectedItems[0].prop1 != "Hello") 
    { // do something 
    } 
} 
+0

あなたが MySelectedItems { のget {_myselectedItemを返します。 集合{_myselectedItem =値};} }公共のObservableCollectionを変更した理由は、公共のObservableCollectionに MySelectedItems { のget {_myselectedItemを返す;} 集合{_myselectedItem =値;} } MyClassをIMyClassに追加しましたか? – Always23

+0

したがって、クラス自体の代わりにクラスのインタフェースまたは記述に対して機能します。この方法では、クラスがIMyClassインターフェイスを実装している限り、どのクラスに与えられているか気にしません。 –

0

prop1とprop2で1つのクラスを作成しました。そのクラスの型をObservableCollections <>に渡してから、そのクラスの別のインスタンスを作成し、それを汎用メソッドに渡しました。

関連する問題