2017-03-25 4 views
0

すべてのコミュニケーションメソッドを配置するためにインターフェイスクラスを作成した後、他のクラスから継承するインターフェイスクラスを作成しましたが、このクラスの属性をチェックする方法インターフェイスのtypeOfに現在のクラスの型を渡す

public KeyValuePair<string, string> CheckForNonNullArguments() 
    { 

     System.Reflection.PropertyInfo[] properties = typeof(BaseBLL).GetProperties(); 
     foreach (System.Reflection.PropertyInfo property in properties) 
      if (property.GetValue(this, null) != null && (!property.GetValue(this, null).Equals("NULL") && !property.GetValue(this, null).ToString().Equals("0")) && !String.IsNullOrEmpty(property.GetValue(this, null).ToString())) 
       return new KeyValuePair<string, string>(property.Name.ToString(), property.GetValue(this, null).ToString()); 
     return new KeyValuePair<string, string>("", ""); 
     //if (property.GetValue(this, null) != null) GetName(() => property); ; 
    } 

「BaseBLL」は、インタフェースクラスの名前で、私はそれを私が継承するたびに変更する必要があり、そうする方法はありますか?私はActivatorを試しましたが、それは仕事をしませんでしたか、それを使用する方法を知らなかったのです。

ありがとうございます。呼び出され

public KeyValuePair<string, string> CheckForNonNullArguments(Type @class) 
{ 

    System.Reflection.PropertyInfo[] properties = @class.GetProperties(); 
    foreach (System.Reflection.PropertyInfo property in properties) 
    { 
     object val = property.GetValue(this, null); // Caching result for better perfs. 
     string str = val?.ToString(); 
     if (val != null && (!str.Equals("NULL") && !str.Equals("0")) && !String.IsNullOrEmpty(str) 
      return new KeyValuePair<string, string>(property.Name.ToString(), str); 
    } 
    return new KeyValuePair<string, string>(string.Empty, string.Empty); 
    //if (property.GetValue(this, null) != null) GetName(() => property); ; 
} 

+0

があなたの方法に 'type'がを渡して、それを呼び出すとき:

public KeyValuePair<string, string> CheckForNonNullArguments<T>() where T : class { System.Reflection.PropertyInfo[] properties = typeof(T).GetProperties(); foreach (System.Reflection.PropertyInfo property in properties) { object val = property.GetValue(this, null); // Caching result for better perfs. string str = val?.ToString(); if (val != null && (!str.Equals("NULL") && !str.Equals("0")) && !String.IsNullOrEmpty(str) return new KeyValuePair<string, string>(property.Name.ToString(), str); } return new KeyValuePair<string, string>(string.Empty, string.Empty); //if (property.GetValue(this, null) != null) GetName(() => property); ; } 

呼び出されます'CheckForNonNullArguments(typeof(BaseBLL))'は、ジョブを実行します –

+0

ありがとうございます(まだ投票できません^^ ") – Angela

答えて

3
このよう

CheckForNonNullArguments(typeof(BaseBLL))

あるいはさらに良い:CheckForNonNullArguments<BaseBLL>()

+0

ありがとうございます。私はそれを使って時間を送ってくれました^^" – Angela

+1

答えが 'GetValue'の戻り値をどのようにキャッシュするか注意してください。これは重要。反射は、特に繰り返し呼び出されると遅くなります。これは、同じ計算を何度も実行するもの(同じ入力を期待し、同じ出力を期待するもの)に適用されます。if(Add(1,1)!= 3 && Add(1,1)!= 14 && Add 1,1)!= 25){...} '、特にAdd()が大きな計算をした場合'int result = Add(1,1); if(result!= 3 && result!= 14 && result!= 25){...} '違いを見ますか?これがどのくらい呼び出されているかに応じて、デリゲートでリフレクションをキャッシュすると効果がありますが、それは別の質問です。 – pinkfloydx33

+2

また、答えにエラーがあります。どちらのメソッドでも、 'val'が' null'であるかどうかを調べる前に 'val.ToString()'を呼び出してNREを投げます。私はそれを 'string str = val.?ToString();に変更します。新しいKeyValuePairを返します。(string.IsNullOrEmpty(str)&&!str.Equals( "NULL")&&!str.Equals( "0")) IsNullOrEmpty'はヌルテストを行いますが、2回は必要ありません)。あなたのニーズ(および可能な入力)に応じて 'string.IsNullOrWhitespace'を代わりに使用し、' NULL '文字列に対して 'Equals'を使用するときに大文字と小文字の区別のオプションを指定することができます – pinkfloydx33

関連する問題