2011-03-30 19 views
1

私のカスタムクラス(名前、年齢、住所などのプロパティを持っています)のリストがあります。リストに "名前"というプロパティがあるかどうかチェックできますか?カスタムクラスの私はプロパティNameのアイテムが存在するかどうかをチェックしたくないのですが、プロパティが存在するかどうかをチェックしたいのです。プロパティがカスタムクラスのリストに存在するかどうかをチェック

これに関する助力?

+0

をそれはあなたが何を意味するかは明らかではありません。あなたは 'List 'を持っていると言っていますが、それぞれの項目に特定のプロパティがあることを確認したいのですか?そのプロパティが 'MyCustomClass'にある場合、そのクラスの厳密に型指定されたリストのすべての項目にあることを前提としています。または、プロパティがnullでも空でもないかどうかを確認することですか?または、他の何か? – David

答えて

7

あなたはFooという名前のクラスを持っていて、プロパティBarでは、次の用いた反射行うことができますが存在するかどうかを確認したい場合は、次のリマインダー@Adamロビンソンのため

bool barExists = typeof(Foo).GetProperties() 
          .Where(x => x.Name == "Bar") 
          .Any(); 

または短いさえ(おかげで):

bool barExists = typeof(Foo).GetProperties().Any(x => x.Name == "Bar") 
+0

私はblah.Where(predicate).Any()とblah.Any(predicate)の間に違いがあるかどうか尋ねることができますか?人々がlinqをどのように利用するかに常に関心があります。乾杯。 –

+1

@ダン:概念では、いいえ、違いはありません。実装では、 'Where(predicate)'を使用すると、 'Any'オーバーロードを呼び出すよりもオーバーヘッドが増えます(カスタム' WhereEnumerableIterator'をスピンアップするため)。大きな違いはありませんが、必要でない場合はなぜ追加しますか? –

+0

うん、過負荷ははるかに簡潔になります。私は通常、Where句で始まり、ソリューションを構築するときにそこから移動します。ソリューションに到達すると、コンパクト化/最適化を忘れることがあります。短い形式が間違いなく好ましい。 – BrokenGlass

2
if(typeof(CustomClass).GetProperties().Where(i => i.Name == FieldYoureLookingFor).Count() > 0) 
{ 
DoSomething(); 
} 
+0

本当の違いはありませんが、 'Any(condition)'を使うのは 'Where(condition).Count()> 0'よりも好ましいです。 –

+0

あなたは正しいですが、私はいつもAny()を忘れています。 –

+0

あなたの返信には本当にありがとうございました。このフィールドに値を保存するにはどうすればいいですか。正しい構文を意味します。 – Janaki

1

ここで、aは指定されたPropertyNameが任意のオブジェクトに存在するかどうかを示すオブジェクト拡張メソッドになります。ここで

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace StackOverflow.MyExtensions 
{ 
    public static class ObjectExtentions 
    { 

     public static Boolean PropertyExists(this object Object, string PropertyName) 
     { 

      var ObjType = Object.GetType(); 

      var TypeProperties = ObjType.GetProperties(); 

      Boolean PropertyExists = TypeProperties 
        .Where(x => x.Name == PropertyName) 
        .Any(); 

      return PropertyExists; 

     } 

    } 
} 

は、使用サンプル行く:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using StackOverflow.MyExtensions; 

namespace StackOverflow 
{ 


    class Person 
    { 
     string _FirstName; // FirstName 

     public string FirstName 
     { 
      get { return _FirstName; } 
      set { _FirstName = value; } 
     } 

     public string LastName; 

    } 


    class Program 
    { 

     static void Main(string[] args) 
     { 

      Person SamplePerson = new Person(); 

      if (SamplePerson.PropertyExists("FirstName")) 
       Console.WriteLine("Yes! Property does exist!"); 
      else 
       Console.WriteLine("Nope, property does not exist on object SamplePerson"); 

      if (SamplePerson.PropertyExists("LastName")) 
       Console.WriteLine("Yes! Property does exist!"); 
      else 
       Console.WriteLine("Nope, property does not exist on object SamplePerson"); 

     } 
    } 

} 

乾杯

関連する問題