【问题标题】:How can i cast into a ObservableCollection<object>我怎样才能转换成 ObservableCollection<object>
【发布时间】:2019-01-09 21:14:44
【问题描述】:

我该如何施放

from ObservableCollection<TabItem> into ObservableCollection<object>

这对我不起作用

(ObservableCollection<object>)myTabItemObservableCollection

【问题讨论】:

  • 也就是所谓的协方差,C#中还没有
  • (对于收藏,在 4.0 中也不可用 - 只是为了清楚)

标签: c# observablecollection covariance


【解决方案1】:

你应该这样复制

return new ObservableCollection<object>(myTabItemObservableCollection);

【讨论】:

  • .net 4 没有解决这个问题,如 Marc Gravell 的帖子中所述。
【解决方案2】:

基本上,你不能。现在不行,not in .NET 4.0。

这里的上下文是什么?你需要什么? LINQ 有Cast&lt;T&gt; 可以将数据作为序列 获取,或者有一些通用方法的技巧(即Foo&lt;T&gt;(ObservalbleCollection&lt;T&gt; col) 等)。

或者你可以只使用非泛型IList?

IList untyped = myTypedCollection;
untyped.Add(someRandomObject); // hope it works...

【讨论】:

    【解决方案3】:

    你可以使用IEnumerable.Cast&lt;T&gt;()

    【讨论】:

      【解决方案4】:

      你不能。 ObservableCollection&lt;TabItem&gt; 不是从 ObservableCollection&lt;object&gt; 派生的。

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

      【讨论】:

        【解决方案5】:

        感谢所有答案,但我想我已经用“帮助方法”解决了这个问题。

        也许对此有更好的方法或 linq 语句。

        private void ConvertTabItemObservableCollection()
        {
          Manager manager = this.container.Resolve<Manager>();
          foreach (var tabItem in manager.ObjectCollection)
          {
            TabItemObservableCollection.Add((TabItem)tabItem);
          }
        }
        

        【讨论】:

        • 由于我们不知道 ObjectCollection 的类型,所以很难回答...
        • 我不明白你的意思?!我有一个 TabItem 我想从类型对象添加到 ObservableCollection 中。 “Manager”是一个全局类,带有 ObservableCollection 我在 prism 应用程序中的任何视图/组件中都需要它。
        • 对不起,我误解了你的答案。 “ObjectCollection”被键入为 object ObservableCollection
        • 是的,但我的回答不是更短吗?并用更少的代码以同样的方式解决了同样的问题
        【解决方案6】:

        我找到的所有示例都没有为我工作,我拼凑了下面的代码,它似乎工作。我有一个通过反序列化 XML 文件创建的层次结构,并且我能够遍历层次结构中的所有对象,但是您可以将其调整为仅遍历一个 ObservableCollection 并将对象作为对象而不是强类型来获取。

        我想为层次结构中的每个属性添加一个 PropertyChangingEventHandler,以便实现撤消/重做功能。

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

        方法就是这样调用的

        TraversalHelper.TraverseAndExecute(object);
        

        如果你只是想创建一个对象的集合,你只需要这段代码

        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
        }
        

        【讨论】:

          【解决方案7】:

          您可以将其转换为 INotifyCollectionChanged;

          喜欢:

          if (myTabItemObservableCollection is INotifyCollectionChanged collection)
          {
             Do Stuff
          }
          

          【讨论】:

            猜你喜欢
            • 2022-10-14
            • 2022-11-29
            • 2016-06-15
            • 2013-07-22
            • 2019-04-28
            • 1970-01-01
            • 2022-11-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多