je vais avoir un peu d'un problème en utilisant la réflexion et l'accès aux collections:Refelection, Collections, Dictonaries Oh mon dieu!
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;
}
Impossible de jeter l'objet de type 'ValueCollection [System.String, Axiom.Core.MovableObject]' taper « System.Collections. Generic.ICollection`1 [System.Object] '.
Pouvez-vous élaborer un peu? Qu'est-ce que vous essayez d'atteindre, que fait votre code et où exactement obtenez-vous le message d'erreur? Les gens se feront un plaisir de vous aider ici, mais s'il vous plaît ne pas simplement vider le code et le message d'erreur et attendez quelqu'un pour le déboguer pour vous ... – Heinzi
Insted de "o.GetType(). GetInterface (" ICollection ")! = Null "vous pouvez utiliser" o est ICollection "ou" o comme ICollection ". Voir http://msdn.microsoft.com/en-us/library/scekt9xw%28VS.71%29.aspx et http://msdn.microsoft.com/en-us/library/cscsdfbt%28v=VS.71 % 29.aspx –