我怎么能投我怎样才能浇铸成的ObservableCollection <object>
from ObservableCollection<TabItem> into ObservableCollection<object>
这并不为我工作
(ObservableCollection<object>)myTabItemObservableCollection
我怎么能投我怎样才能浇铸成的ObservableCollection <object>
from ObservableCollection<TabItem> into ObservableCollection<object>
这并不为我工作
(ObservableCollection<object>)myTabItemObservableCollection
你应该复制这样
return new ObservableCollection<object>(myTabItemObservableCollection);
你忘了``new`关键字 – 2009-07-29 08:35:38
你不能。 ObservableCollection<TabItem>
并非来自ObservableCollection<object>
。
如果你解释为什么你想或许我们可以指出一个你可以使用的替代接口。
基本上,你不能。现在不是,并且not in .NET 4.0。
这是什么情况?你需要什么? LINQ有Cast<T>
它可以让你的数据作为序列,或者有一些技巧与通用的方法(即Foo<T>(ObservalbleCollection<T> col)
等)。
或者您可以使用非通用的IList
?
IList untyped = myTypedCollection;
untyped.Add(someRandomObject); // hope it works...
你可以使用IEnumerable.Cast<T>()
thanx的所有答案,但我想我有解决这个问题的自我与“helpermethode”。
也许有更好的方法或linq声明。
private void ConvertTabItemObservableCollection()
{
Manager manager = this.container.Resolve<Manager>();
foreach (var tabItem in manager.ObjectCollection)
{
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
}
}
}
}
}
}
的方法就被称为像这样
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
}
这就是所谓的协方差此位,并它尚未在C# – 2009-07-29 08:24:45
(和收集,将不会在4.0中可用 - 只是要清楚) – 2009-07-29 08:27:29