Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何才能将其转换为可观察的集合<;对象>;_C#_Observablecollection_Covariance - Fatal编程技术网

C# 我如何才能将其转换为可观察的集合<;对象>;

C# 我如何才能将其转换为可观察的集合<;对象>;,c#,observablecollection,covariance,C#,Observablecollection,Covariance,我该怎么投 from ObservableCollection<TabItem> into ObservableCollection<object> 从ObservableCollection到ObservableCollection 这对我不起作用 (ObservableCollection<object>)myTabItemObservableCollection (ObservableCollection)myTabItemObservableCo

我该怎么投

from ObservableCollection<TabItem> into ObservableCollection<object>
从ObservableCollection到ObservableCollection
这对我不起作用

(ObservableCollection<object>)myTabItemObservableCollection
(ObservableCollection)myTabItemObservableCollection

你不能<代码>ObservableCollection不是从
ObservableCollection
派生的


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

您应该这样复制

return new ObservableCollection<object>(myTabItemObservableCollection);
TraversalHelper.TraverseAndExecute(object);
返回新的ObservableCollection(myTabItemObservableCollection);

基本上,你不能。现在不行,还有

这里的背景是什么?你需要什么?LINQ有
Cast
,可以将数据作为一个序列获取,或者有一些通用方法的技巧(例如
Foo(ObservalbleCollection col)
等)

或者您可以只使用非通用的
IList

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

您可以使用
IEnumerable.Cast()
thanx作为所有答案,但我想我已经用“helpermethod”自行解决了这个问题

也许有更好的方法或linq语句来实现这一点

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

我发现的示例中没有一个对我有效,我拼凑了下面的代码,似乎有效。我有一个层次结构,它是通过反序列化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
                    }
                }
            }
        }
    }
}
如果你只想创建一个对象集合,你只需要这段代码

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
}
observeCollection typedField=migration.FileDescriptions[0]。入站[0]。表[0]。表[0]。字段[0]。字段;//这是强类型的decaration,一个字段对象的集合
对象myObject=typedField;//声明为对象
Int32 propertyValueCount=(Int32)myObject.GetType().GetMethod(“get_Count”).Invoke(myObject,null);//此集合中有多少个对象
for(int i=0;i
您可以将其强制转换为INotifyCollectionChanged

比如:

if (myTabItemObservableCollection is INotifyCollectionChanged collection)
{
   Do Stuff
}

这就是所谓的协方差,它在C#中还不可用(对于集合,也不会在4.0中可用-只是为了澄清),因为我们不知道ObjectCollection的类型是什么,这很难回答…我不明白你的意思?!我有一个TabItem,我想从类型对象添加到ObservableCollection中。“Manager”是一个全局类,具有我在prism应用程序的任何视图/组件中所需的可观察集合。啊,对不起,我误解了你的答案。“ObjectCollection”类型为object ObservableCollectionyea,但我的答案不是更短吗?用更少的代码以同样的方式解决了同样的问题。net 4并不能解决Marc Gravell的文章中详细描述的这个问题。