2009-10-01 9 views
8

ジェネリックリスト であれば、私はリフレクション経由でオブジェクトのすべてのプロパティをループしてる決定: プロパティが反射し、ループのリスト項目を経由して

For Each p As PropertyInfo In values.[GetType]().GetProperties() 
    If p.CanRead Then 
     'Do stuff 
    End If 
Next 

誰もが問題のプロパティがAであるかどうかを確認する方法を教えてもらえますジェネリックリスト(Of T)?それが必要な場合は、リスト自体をループする必要があります。

私はGetTypeとTypeOfを試してみましたが、何も問題なく動作していません。

ありがとうございました。明確にするために

****更新と明確化**

、私はこのジェネリックを維持したいです。私はTの型を指定したくないので、リスト項目をループし、各項目のToStringメソッドを呼び出す必要があります。 Tは、いくつかの異なるタイプ(アプリケーション固有の参照タイプ)の1つです。型を指定せずにこれを行うことは可能ですか?

(.NET 2.0とVB.NET 2005)

答えて

4

はVB.NetでRoatinsの答えは、完全なコンソールアプリケーション

ここ
Imports System 
Imports System.Reflection 
Imports System.Collections.Generic 
Imports System.Collections 

Namespace ReflectionTest 
    Public Class Object1 
     Public Overloads Overrides Function ToString() As String 
      Return "This is Object 1" 
     End Function 
    End Class 
    Public Class Object2 
     Public Overloads Overrides Function ToString() As String 
      Return "This is Object 2" 
     End Function 
    End Class 

    Public Class ContainerClass 
     Public Property objects() As List(Of Object) 
      Get 
      End Get 
      Set 
      End Set 
     End Property 
     Public Property propA() As Integer 
      Get 
      End Get 
      Set 
      End Set 
     End Property 
     Public Property propB() As String 
      Get 
      End Get 
      Set 
      End Set 
     End Property 
     Public Property propC() As String() 
      Get 
      End Get 
      Set 
      End Set 
     End Property 
    End Class 
    Class Program 
     Shared Sub Main(args As String()) 
      ' Sample class instance 
      Dim c As New ContainerClass() 

      ' Add some sample data 
      c.objects = New List(Of Object)() 
      c.objects.Add(New Object1()) 
      c.objects.Add(New Object2()) 

      Dim props As PropertyInfo() = c.[GetType]().GetProperties() 

      For Each p As PropertyInfo In props 
       If GetType(IList).IsAssignableFrom(p.PropertyType) AndAlso p.PropertyType.IsGenericType Then 
        Dim item As IList = DirectCast(p.GetValue(c, Nothing), IList) 
        If item <> Nothing Then 
         For Each o As Object In item 
          Console.WriteLine(o.ToString()) 
         Next 
        End If 
       End If 
      Next 
      Console.ReadLine() 
     End Sub 


    End Class 
End Namespace 
+0

うーん、これはstackoverflowの上で行うことは違法ではありません? –

+1

ただ人を助けようとしています。私はconverter.telerik.comを使用しました – Ryu

-1
if p.PropertyType = TypeOf List(Of T) then... 
+0

訂正:TypeOf関数のp.PropertyTypeは(Tの)リストされている場合、... – Simon

13

は、この完全なコンソールアプリケーションを試してみてください。申し訳ありませんが、C#です。ここで

using System; 
using System.Reflection; 
using System.Collections.Generic; 
using System.Collections; 

namespace ReflectionTest 
{ 
    public class Object1 
    { 
     public override string ToString() 
     { 
      return "This is Object 1"; 
     } 
    } 
    public class Object2 
    { 
     public override string ToString() 
     { 
      return "This is Object 2"; 
     } 
    }  

    public class ContainerClass 
    { 
     public List<object> objects { get; set; } 
     public int propA { get; set; } 
     public string propB { get; set; } 
     public string[] propC { get; set; } 
    } 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Sample class instance 
      ContainerClass c = new ContainerClass(); 

      // Add some sample data 
      c.objects = new List<object>(); 
      c.objects.Add(new Object1()); 
      c.objects.Add(new Object2()); 

      PropertyInfo[] props = c.GetType().GetProperties(); 

      foreach (PropertyInfo p in props) 
      { 
       if (typeof(IList).IsAssignableFrom(p.PropertyType) 
        && p.PropertyType.IsGenericType) 
       { 
        IList item = (IList)p.GetValue(c, null); 
        if (item != null) 
        { 
         foreach (object o in item) 
         { 
          Console.WriteLine(o.ToString()); 
         } 
        } 
       } 
      } 
      Console.ReadLine(); 
     } 


    }   
} 
0

あなたはVB.NETで行きます。 (私は.NET 4.5を使用します)。 あなたの元のオブジェクトが変数名=のMyDataと(Tの)リストである場合は、上記のコードその後、

Dim CurCols() as PropertyInfo = MyData.GetType.GetGenericArguments()(0).GetProperties 

はMyDataにリスト内のすべてのプロパティを提供します。

メインリスト(MyData)のプロパティをループし、単一のプロパティ自体がリストタイプであるかどうかを調べるには、以下のループを使用します。要件に基づいて必要でない場合、IsGenericTypeチェックを削除することができます。

For Each iCol In CurCols 
    Dim colType as Type = iCol.PropertyType 
    If colType.IsGenericType AndAlso colType.GetGenericTypeDefinition = GetType(List(Of)) Then 
     MsgBox(iCol.Name.ToString & " Is a List Type.") 
    End If 
Next 
関連する問題