2010-11-22 4 views
0

私は、リフレクションを使用して、問題のビットを持っやコレクションにアクセスしています:Refelection、Collections、Dictonariesああ私は!

XmlElement xmlObject = Scene.CreateElement("Object"); 
Type Target = obj.GetType(); 

... xml code here 

PropertyInfo[] props = Target.GetProperties(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.Static); 

foreach (PropertyInfo prop in props) 
{ 
Type propType = prop.PropertyType; 

if ((propType.IsPublic && propType.IsValueType && prop.CanRead && prop.CanWrite) 
    || PropertyNameExceptions.Contains(prop.Name) 
    || PropertyTypeExceptions.Contains(prop.PropertyType.Name)) 
{ 

    object result = null; 
    try 
    { 
    result = prop.GetValue(obj, null); 
    } 
    catch 
    { 

    } 
    } 

else if (isCollection(result)) 
{ 

    Type pType = result.GetType(); 
    PropertyInfo[] pTypeInfo = pType.GetProperties(); 

    ICollection<object> rCollection = null; 
    try 
    { 
    rCollection = (ICollection<object>)prop.GetValue(obj, null); 
    } 
    catch 
    { 

    } 

    foreach (object o in rCollection) 
    { 
    ObjectToXML(o, xmlPropertyObject); 
    } 
} 
} 

private bool isCollection(object o) 
{ 
if (o.GetType().GetInterface("ICollection") != null) 
{ 
return true; 
} 

return false; 
} 

「にSystem.Collectionsを入力するタイプ「ValueCollection [可能System.String、Axiom.Core.MovableObject]」のオブジェクトをキャストすることができません。 Generic.ICollection`1 [System.Object] '。 ICollectionの非ジェネリックバージョンはICollection<Object>にキャストしようhapillyオブジェクトによってimplementendとされている場合は、テスト

+1

少し詳しく説明できますか?あなたは何を達成しようとしていますか?あなたのコードは何をしていますか、どこにエラーメッセージが表示されますか?人々はここで喜んでお手伝いしますが、単にコードとエラーメッセージをダンプして、誰かがあなたのためにそれをデバッグすることを期待しないでください... – Heinzi

+0

"o.GetType()。GetInterface(" ICollection ")!= nullのInsted "o is ICollection"または "o as ICollection"を使用できます。 http://msdn.microsoft.com/en-us/library/scekt9xw%28VS.71%29.aspxおよびhttp://msdn.microsoft.com/en-us/library/cscsdfbt%28v=VS.71を参照してください。 %29.aspx –

答えて

2

...

どちらのテストオブジェクトは、本当にICollection<Object>実装する場合:

private bool isCollection(object o) 
{ 
    return o is ICollection<object>; 
} 

rCollection = ((IEnumerable)prop.GetValue(obj, null)).OfType<Object>().ToList(); 
関連する問題