2009-10-01 13 views
8

मैं प्रतिबिंब के माध्यम से एक वस्तु के सारे गुण पाशन कर रहा हूँ के माध्यम से है:निर्धारित करें यदि संपत्ति सामान्य सूची <of T> परावर्तन और पाश सूची आइटम

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

किसी को मुझे बताओ कैसे निर्धारित करने के लिए सकते हैं कि क्या सवाल में संपत्ति एक है जेनेरिक सूची (टी)? अगर मुझे सूची को लूप करने की ज़रूरत है।

मैंने गेट टाइप और टाइपऑफ के साथ प्रयोग किया है लेकिन कुछ भी काम करने में कामयाब नहीं रहा है।

धन्यवाद।

**** अद्यतन और स्पष्टीकरण **

स्पष्ट करने के लिए, मैं इस सामान्य रखना चाहते हैं। मैं टी के प्रकार को निर्दिष्ट नहीं करना चाहता, मुझे सूची आइटम लूप करने और प्रत्येक आइटम पर ToString विधि को कॉल करने की आवश्यकता है। टी कई अलग-अलग प्रकारों में से एक हो सकता है (एप्लिकेशन विशिष्ट संदर्भ प्रकार)। क्या यह प्रकार निर्दिष्ट किए बिना ऐसा करना संभव है?

(नेट 2.0 के साथ VB.NET 2005)

उत्तर

4

यहाँ

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

Uhhh, नहीं stackoverflow पर करने के लिए इस गैर कानूनी है है ?? –

+1

बस लड़के की मदद करने की कोशिश कर रहा है। मैंने कनवर्टर.telerik.com का इस्तेमाल किया – Ryu

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

सुधार: यदि typeof p.PropertyType सूची (टी का) फिर ... – Simon

13

यह पूरा सांत्वना आवेदन की कोशिश करें। क्षमा करें यह सी # में है।

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 में Roatins जवाब है, पूरा कंसोल आवेदन यहाँ आप VB.NET में जाना है। (मैं .NET 4.5 का उपयोग करता हूं)। अपने मूल वस्तु चर नाम के साथ (टी की) सूची = MyData है, तो

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 
संबंधित मुद्दे