2009-04-02 2 views
1

Reflectionを使用してジェネリックの種類のプロパティを引き出し、属性を探しているジェネリッククラスがあります。私はそれぞれのプロパティのそれぞれについて同じことをするために各プロパティに再帰しています。私の問題は、何らかのコレクションプロパティ(コレクションであるプロパティ)またはICollectionプロパティに来るときです。 GetValueから返された値を特定の型にキャストすることはできません(IEnumerableにキャストしようとしましたが、一般的なIEnumerableでは機能しません)。ここでReflectを持つGenericリスト/コレクションの処理項目

はもう少し理解するためのいくつかのコードです:

public class NotificationMessageProcessor<T> : INotificationProcessor<T> 
    { 
      IList<string> availableTags = new List<string>(); 

      public string ReplaceNotificationTags<T>(string message, T instance) 
      { 

      LoadTagValues(instance); 
      return ReplaceTags(message); 

      } 

      private string ReplaceTags(string message) 
      { 
      foreach (KeyValuePair<string, string> tagVal in tagValues) 
      { 
       message = message.Replace(string.Format("<{0}>", tagVal.Key),  tagVal.Value); 
      } 
      return message; 
      } 

      private void LoadTagValues(object val) 
      { 
       Type elementType = val.GetType(); 
       PropertyInfo[] typeProperties = elementType.GetProperties(); 
       foreach (PropertyInfo prop in typeProperties) 
       { 

       NotificationTag[] tags =  (NotificationTag[])prop.GetCustomAttributes(typeof(NotificationTag), false); 
       if (tags != null && tags.Length > 0) 
       { 
        string tagName = tags[0].TagName; 
        object propValue = prop.GetValue(val, null); 
        string propTypeString = prop.PropertyType.FullName; 
        tagName = prop.ReflectedType.Name + "." + tagName; 
        if (propValue != null) 
        { 
         tagValues.Add(tagName, propValue.ToString()); 
        } 

        if (propValue != null) 
        { 
         if (!prop.PropertyType.IsPrimitive) 
         { 
         LoadTagValues(propValue); 
         } 
        } 
       } 
       else 
       { 
        if (!prop.PropertyType.IsPrimitive) 
        { 
        object propValue = null; 

         if (prop.GetGetMethod().GetParameters().Count() == 0) 
         { 
          propValue = prop.GetValue(val, null); 
         } 
         else 
         { 
          //have a collection...need to process but do not know how many in collection.... 
           propValue = prop.GetValue(val, new object[] { 0 }); 

         } 
        if (propValue != null) 
        { 
         LoadTagValues(propValue); 
        } 
       } 
      } 
     } 
    } 




NotificationMessageProcessor<User> userProcessor = new NotificationMessageProcessor(); 
    userProcessor.ReplaceNotificationTags<User>(someMessage, instanceOfUser); 

ユーザーオブジェクトは私が理解から、適切な属性

+0

あなたが実際にやりたいことについてのいくつかの情報は役に立ちます - すべては抽象的なもので少し奇妙です。 –

+0

私はいくつかのコードを追加しました.... – CSharpAtl

+0

私はそれを考え出しました.... – CSharpAtl

答えて

0

を助け、私はIEnumerableをにキャストをしています

List<OfObject> myCollection = new List<OfObject>; 
myCollection = (List<OfObject>)objPropertyInfo.GetValue(List<ObjectHere>, Nothing); 

希望を、私は問題を抱えた時に間違ったオブジェクトをキャストしようとしていました。

0

を持っている、あなたが必要としないので、非ジェネリックIEnumerableを行いますとにかくタイプ情報。

0

プロパティのコレクション型を実際のコレクションの型にキャストしてみることはできますか?

次のようなものをやっている:これは

+0

私はタイプを知りません.....リフレクションを使用しているクラスは汎用クラスです – CSharpAtl

+0

記事http:// objectmix。 com/csharp/151909-create-empty-list-type-name.htmlあなたに役立つかもしれない – w4ymo

0

最終回答(Anton Gogolev)は最高です。例えば:

私はこの一般的な機能を持っていた:

自体も( List<[Unknown Model Type]> ...)

ジェネリックリストを返され、私はそれから単一の結果を得るためにどこにでも見えたが、私

var fieldFetchedData = fieldQueryHandler.GetType().GetMethod("GetFilter").MakeGenericMethod(selectedParameter.ParameterType).Invoke(fieldQueryHandler,fieldParameters.ToArray()); 

それをする前に何かをキャストしなければならなかったし、ユーザーや他のモデルクラス(私はそれがすべきものがわからない)を定義する方法はなかった。generics。私はそれを見たら、私は自分自身に言った、私は多くの方法を試してみました、またこれを試してみて、そして私はこのようにそれをやってみましょう:

IEnumerator enumeratorFetchedData = ((IEnumerable) fieldFetchedData).GetEnumerator(); 
object obj = enumeratorFetchedData.MoveNext()? enumeratorFetchedData.Current:null; 

とそれが必要として、それが働いていました!

+0

悪い英語Jasseのために悲しい:)それは私の言語ではなく、私は正式な方法ではなく世界中の人々と話すことによってそれを学ぶ –

関連する問題