Comment puis-je lancer
from ObservableCollection<TabItem> into ObservableCollection<object>
cela ne fonctionne pas pour moi
(ObservableCollection<object>)myTabItemObservableCollection
Comment puis-je lancer
from ObservableCollection<TabItem> into ObservableCollection<object>
cela ne fonctionne pas pour moi
(ObservableCollection<object>)myTabItemObservableCollection
Aucun des exemples que j'ai trouvés n'a fonctionné pour moi, j'ai bricolé le code ci-dessous et il semble fonctionner. J'ai une hiérarchie qui est créée par la désérialisation d'un fichier XML et je suis capable de boucler à travers tous les objets dans la hiérarchie, mais vous pouvez adapter ceci pour juste boucler à travers une ObservableCollection et obtenir les objets comme des objets et non fortement typés.
Je veux ajouter un PropertyChangingEventHandler à chaque propriété de la hiérarchie afin de pouvoir mettre en œuvre la fonctionnalité annuler/refaire.
public static class TraversalHelper
{
public static void TraverseAndExecute(object node)
{
TraverseAndExecute(node, 0);
}
public static void TraverseAndExecute(object node, int level)
{
foreach (var property in node.GetType().GetProperties())
{
var propertyValue = node.GetType().GetProperty(property.Name).GetGetMethod().Invoke(node, null); // Get the value of the property
if (null != propertyValue)
{
Console.WriteLine("Level=" + level + " : " + property.Name + " :: " + propertyValue.GetType().Name); // For debugging
if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(ObservableCollection<>)) // Check if we are dealing with an observable collection
{
//var dummyvar = propertyValue.GetType().GetMethods(); // This was just used to see which methods I could find on the Collection
Int32 propertyValueCount = (Int32)propertyValue.GetType().GetMethod("get_Count").Invoke(propertyValue, null); // How many objects in the collection
level++;
for (int i = 0; i < propertyValueCount; i++) // Loop over all objects in the Collection
{
object properyValueObject = (object)propertyValue.GetType().GetMethod("get_Item").Invoke(propertyValue, new object[] { i }); // Get the specified object out of the Collection
TraverseAndExecute(properyValueObject, level); // Recursive call in case this object is a Collection too
}
}
}
}
}
}
La méthode est simplement appelée comme ceci
TraversalHelper.TraverseAndExecute(object);
Si vous souhaitez simplement créer une collection d'objets, il vous suffit d'utiliser ce bout de code
ObservableCollection<Field> typedField = migration.FileDescriptions[0].Inbound[0].Tables[0].Table[0].Fields[0].Field; // This is the strongly typed decalaration, a collection of Field objects
object myObject = typedField; // Declare as object
Int32 propertyValueCount = (Int32)myObject.GetType().GetMethod("get_Count").Invoke(myObject, null); // How many objects in this Collection
for (int i = 0; i < propertyValueCount; i++) // Loop over all objects in the Collection
{
object properyValueObject = (object)myObject.GetType().GetMethod("get_Item").Invoke(myObject, new object[] { i }); // Get the specified object out of the Collection, in this case a Field object
// Add the object to a collection of objects, or whatever you want to do with object
}
Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.