मैं एक अनुक्रमित संपत्ति है कि मैं केवल प्रतिबिंब के माध्यम से करने के लिए उपयोग किया है से अधिक itterate करना चाहते हैं,पुनरावृत्ति (प्रतिबिंब)
लेकिन (और मैं वहाँ शायद एक शर्मनाक सरल जवाब है कि पूरी जानकारी में यह कहते हैं, एमएसडीएन/Google असफल = /) TargetInvocationException
फेंकने तक PropertyInfo.GetValue(prop, counter)
पर काउंटर बढ़ाने के अलावा मैं किसी तरीके से नहीं ढूंढ सकता/सोच सकता हूं।
आला:
foreach (PropertyInfo prop in obj.GetType().GetProperties())
{
if (prop.GetIndexParameters().Length > 0)
{
// get an integer count value, by incrementing a counter until the exception is thrown
int count = 0;
while (true)
{
try
{
prop.GetValue(obj, new object[] { count });
count++;
}
catch (TargetInvocationException) { break; }
}
for (int i = 0; i < count; i++)
{
// process the items value
process(prop.GetValue(obj, new object[] { i }));
}
}
}
अब, कुछ मुद्दों पर .. इस ... बहुत बदसूरत .. समाधान के साथ देखते हैं
क्या हुआ अगर यह बहु-आयामी या उदाहरण के लिए पूर्णांक द्वारा अनुक्रमित नहीं है .. ।
यहाँ परीक्षण कोड मैं कोशिश करते हैं और अगर कोई यह की जरूरत है काम कर इसे पाने के लिए उपयोग कर रहा हूँ। यदि कोई दिलचस्पी लेता है तो मैं एक कस्टम कैशिंग सिस्टम बना रहा हूं और .क्वल्स इसे काट नहीं देता है।
static void Main()
{
object str = new String(("Hello, World").ToArray());
process(str);
Console.ReadKey();
}
static void process(object obj)
{
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
// if this obj has sub properties, apply this process to those rather than this.
if (properties.Length > 0)
{
foreach (PropertyInfo prop in properties)
{
// if it's an indexed type, run for each
if (prop.GetIndexParameters().Length > 0)
{
// get an integer count value
// issues, what if it's not an integer index (Dictionary?), what if it's multi-dimensional?
// just need to be able to iterate through each value in the indexed property
int count = 0;
while (true)
{
try
{
prop.GetValue(obj, new object[] { count });
count++;
}
catch (TargetInvocationException) { break; }
}
for (int i = 0; i < count; i++)
{
process(prop.GetValue(obj, new object[] { i }));
}
}
else
{
// is normal type so.
process(prop.GetValue(obj, null));
}
}
}
else
{
// process to be applied to each property
Console.WriteLine("Property Value: {0}", obj.ToString());
}
}
क्या 'वस्तु str = नया स्ट्रिंग का उद्देश्य है ((" हैलो, दुनिया ") toArray()।)' है? –
बस मेरे फ़ंक्शन को पास करने के लिए एक उदाहरण चर ... स्ट्रिंग/स्ट्रिंग को परिभाषित करने के विभिन्न तरीकों का प्रयास कर रहा था और इसे थोड़ा अजीब पर छोड़ दिया ... 'object str =" हैलो, वर्ल्ड! ";' बस काम करता है। –
यदि मेरे पास STRING कुंजी हैं, तो पूर्णांक नहीं है? मैं उनके नाम नहीं जानता। उन्हें कैसे ढूंढें और उपयोग करें? – Alexander