12

How can i cast

from ObservableCollection<TabItem> into ObservableCollection<object>

this doesnt work for me

(ObservableCollection<object>)myTabItemObservableCollection

You can't. ObservableCollection<TabItem> does not derive from ObservableCollection<object>.

If you explain why you would want to perhaps we can point out an alternative interface you can use.

4

7 回答 7

13

you should copy like this

return new ObservableCollection<object>(myTabItemObservableCollection);
于 2009-07-29T08:25:55.183 回答
12

Basically, you can't. Not now, and not in .NET 4.0.

What is the context here? What do you need? LINQ has Cast<T> which can get you the data as a sequence, or there are some tricks with generic methods (i.e. Foo<T>(ObservalbleCollection<T> col) etc).

Or you can just use the non-generic IList?

IList untyped = myTypedCollection;
untyped.Add(someRandomObject); // hope it works...
于 2009-07-29T08:26:25.993 回答
4

you could use IEnumerable.Cast<T>()

于 2009-07-29T08:26:59.423 回答
0

thanx for all answers, but I think I have solve this problem self with a "helpermethode".

Perhaps has any a better method or a linq statement for this.

private void ConvertTabItemObservableCollection()
{
  Manager manager = this.container.Resolve<Manager>();
  foreach (var tabItem in manager.ObjectCollection)
  {
    TabItemObservableCollection.Add((TabItem)tabItem);
  }
}
于 2009-07-29T08:50:32.950 回答
0

None of the examples I have found have worked for me, I have cobbled together the below code and it seems to work. I have a hierarchy that is created by deserializing an XML file and I am able to loop through all the objects in the hierarchy, but you can adapt this to just loop through one ObservableCollection and get the objects as objects and not strongly typed.

I want to add a PropertyChangingEventHandler to every property in the hierarchy so that I can implement undo/redo functionality.

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
                    }
                }
            }
        }
    }
}

The method is just called like this

TraversalHelper.TraverseAndExecute(object);

If you just want to create a collection of objects you just need this bit of 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
}
于 2017-07-13T09:37:43.053 回答
0

你不能。ObservableCollection<TabItem>不衍生自ObservableCollection<object>

如果您解释了为什么要这样做,也许我们可以指出您可以使用的替代界面。

于 2009-07-29T08:25:28.490 回答
-1

You can Cast it as INotifyCollectionChanged;

Like:

if (myTabItemObservableCollection is INotifyCollectionChanged collection)
{
   Do Stuff
}
于 2019-01-09T13:14:44.613 回答