2009-07-29 23 views
11

Làm thế nào tôi có thể đúcLàm thế nào tôi có thể đúc thành một ObservableCollection <object>

from ObservableCollection<TabItem> into ObservableCollection<object> 

doesnt làm việc này cho tôi

(ObservableCollection<object>)myTabItemObservableCollection 
+2

đó được gọi là hiệp phương sai, và nó chưa có sẵn trong C# –

+1

(và cho các bộ sưu tập, sẽ không có sẵn trong 4.0 hoặc - chỉ để được rõ ràng) –

Trả lời

12

bạn nên sao chép như thế này

return new ObservableCollection<object>(myTabItemObservableCollection); 
+0

bạn quên từ khóa 'new' –

+0

thanks Dr ew, tôi đã thêm nó –

+1

.net 4 KHÔNG giải quyết vấn đề này như được nêu chi tiết trong bài đăng của Marc Gravell. – MrSlippers

0

Bạn không thể. ObservableCollection<TabItem> không xuất phát từ ObservableCollection<object>.

Nếu bạn giải thích lý do tại sao bạn muốn có lẽ chúng tôi có thể chỉ ra một giao diện thay thế bạn có thể sử dụng.

11

Về cơ bản, bạn có thể không. Không phải bây giờ và not in .NET 4.0.

Ngữ cảnh ở đây là gì? Bạn cần gì? LINQ có Cast<T> có thể lấy cho bạn dữ liệu dưới dạng một chuỗi hoặc có một số thủ thuật với các phương pháp chung (ví dụ: Foo<T>(ObservalbleCollection<T> col) v.v.).

Hoặc bạn chỉ có thể sử dụng không chung chung IList?

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

bạn có thể sử dụng IEnumerable.Cast<T>()

0

thanx cho tất cả các câu trả lời, nhưng tôi nghĩ rằng tôi phải giải quyết vấn đề tự này với một "helpermethode".

Có lẽ có phương pháp hay hơn hoặc câu lệnh linq cho điều này.

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

Vì chúng ta không biết ObjectCollection được gõ như thế nào, điều đó khá khó trả lời ... –

+0

Tôi không hiểu ý bạn là gì ?! Tôi có một TabItem những gì tôi nên thêm vào một ObservableCollection từ đối tượng loại. "Người quản lý" là một lớp học toàn cầu với ObservableCollection những gì tôi cần trong bất kỳ chế độ xem/thành phần nào trong ứng dụng lăng kính của mình. –

+0

Ah xin lỗi tôi đã hiểu lầm ur Trả lời. "ObjectCollection" được nhập là đối tượng ObservableCollection

0

Không có ví dụ nào tôi tìm thấy đã làm việc cho tôi, tôi đã trộn lẫn mã bên dưới và có vẻ như nó hoạt động. Tôi có một hệ thống phân cấp được tạo bởi deserializing một tệp XML và tôi có thể lặp qua tất cả các đối tượng trong hệ thống phân cấp, nhưng bạn có thể điều chỉnh điều này để chỉ lặp qua một ObservableCollection và lấy các đối tượng như đối tượng và không gõ mạnh.

Tôi muốn thêm PropertyChangingEventHandler vào mọi thuộc tính trong cấu trúc phân cấp để tôi có thể triển khai chức năng hoàn tác/làm lại.

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

Phương pháp này chỉ được gọi như thế này

TraversalHelper.TraverseAndExecute(object); 

Nếu bạn chỉ muốn tạo ra một bộ sưu tập của các đối tượng bạn chỉ cần chút mã này

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ác vấn đề liên quan