/ / Ein Objekt als ObservableCollection <Objekt> darstellen - c #, .net, observablecollection

Ein Objekt als ObservableCollection <Objekt> casten - c #, .net, observablecollection

Ich versuche, eine Methode zu schreiben, die alle ObservableCollections in einem viewModel abruft und jedes als ein ObservableCollection<object>. Durch Nachdenken konnte ich jedes bekommen ObservableCollection<T> als ein Objekt, aber ich habe Schwierigkeiten, dieses Objekt zu einem ObservableCollection<object>. Hier ist mein Code soweit:

var props = viewModel.GetType().GetProperties();

Type t = viewModel.GetType();

foreach (var prop in props)
{
if (prop.PropertyType.Name == "ObservableCollection`1")
{
Type type = prop.PropertyType;
var property = (t.GetProperty(prop.Name)).GetValue(viewModel);

// cast property as an ObservableCollection<object>
}
}

Weiß jemand wie ich vorgehen soll?

Antworten:

2 für die Antwort № 1

Es ist eine schlechte Idee, den Typnamen mit einem String zu vergleichen. Um es zu behaupten, ist es ein ObservableCollectionkönnen Sie Folgendes verwenden:

if (prop.PropertyType.IsGenericType &&
prop.PropertyType.GetGenericTypeDefinition() == typeof(ObservableCollection<>))

Der Wert kann extrahiert und wie folgt transformiert werden:

foreach (var prop in viewModel.GetType().GetProperties())
{
if (prop.PropertyType.IsGenericType &&
prop.PropertyType.GetGenericTypeDefinition() == typeof(ObservableCollection<>))
{
var values = (IEnumerable)prop.GetValue(viewModel);

// cast property as an ObservableCollection<object>
var collection = new ObservableCollection<object>(values.OfType<object>());
}
}

Wenn Sie sie in einer Sammlung kombinieren möchten, können Sie dies tun:

var values = viewModel.GetType().GetProperties()
.Where(p => p.PropertyType.IsGenericType)
.Where(p => p.PropertyType.GetGenericTypeDefinition() == typeof(ObservableCollection<>))
.Select(p => (IEnumerable)p.GetValue(viewModel))
.SelectMany(e => e.OfType<object>());
var collection = new ObservableCollection<object>(values);

1 für die Antwort № 2

Antwort für diese Frage ist hier: https://stackoverflow.com/a/1198760/3179310

Aber um es für Ihren Fall klar zu machen:

if (prop.PropertyType.Name == "ObservableCollection`1")
{
Type type = prop.PropertyType;
var property = (t.GetProperty(prop.Name)).GetValue(viewModel);

// cast property as an ObservableCollection<object>
var col = new ObservalbeCollection<object>(property);
// if the example above fails you need to cast the property
// from "object" to an ObservableCollection<T> and then execute the code above
// to make it clear:
var mecol = new ObservableCollection<object>();
ICollection obscol = (ICollection)property;
for(int i = 0; i < obscol.Count; i++)
{
mecol.Add((object)obscol[i]);
}
// the example above can throw some exceptions but it should work in most cases
}

0 für die Antwort № 3

Du könntest das benutzen Cast<T>() Erweiterungsmethode, aber vergessen Sie nicht, mit diesemMethode (unten) Dies wird eine neue Instanz erstellen, so dass Ereignisse des Originals nicht funktionieren. Wenn Sie weiterhin Ereignisse erhalten möchten, sollten Sie einen Wrapper erstellen.

var prop = viewModel.GetType("ObservableCollection`1");

var type = prop.PropertyType;
var propertyValue = (t.GetProperty(prop.Name)).GetValue(viewModel);

// cast property as an ObservableCollection<object>
var myCollection = new ObservableCollection<object>(
((ICollection)propertyValue).Cast<object>());

}