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を取得する必要があります。
ご迷惑をおかけして申し訳ございません。おかげさまで
[ジェネリッククラスのプロパティを取得]の可能な複製(https://stackoverflow.com/questions/14129421/get-property-of-generic-class) –
******注:最後の文の訂正:渡された値がObservableCollectionの場合、私はprop1とprop2を取得する必要があります。 –
Always23
@ Kitty23コメントに間違いはありません。投稿を編集してください。 – maccettura