4 using System.Collections;
5 using System.Collections.Generic;
8 using SiliconStudio.Core.Extensions;
9 using SiliconStudio.Core.Reflection;
11 namespace SiliconStudio.Quantum.References
18 private readonly List<ObjectReference> references =
new List<ObjectReference>();
19 private readonly List<object> indices =
new List<object>();
20 private readonly Type elementType;
24 Reference.CheckReferenceCreationSafeGuard();
25 Type = enumerableType;
29 elementType = enumerableType.GetInterface(typeof(
IDictionary<,>)).GetGenericArguments()[1];
31 elementType = enumerableType.GetInterface(typeof(
IEnumerable<>)).GetGenericArguments()[0];
33 elementType = typeof(
object);
39 public object ObjectValue {
get;
private set; }
42 public Type Type {
get;
private set; }
45 public object Index {
get;
private set; }
50 public bool IsDictionary {
get {
return ObjectValue is
IDictionary || ObjectValue.GetType().HasInterface(typeof(
IDictionary<,>)); } }
53 public int Count {
get {
return references.Count; } }
56 public ObjectReference this[
object index] {
get {
return references.Single(x => Equals(x.Index, index)); } }
71 return references.Any(x => Equals(x.Index, index));
75 public void Refresh(
object newObjectValue)
77 if (!(newObjectValue is
IEnumerable))
throw new ArgumentException(
@"The object is not an IEnumerable",
"newObjectValue");
79 ObjectValue = newObjectValue;
84 ? ((IEnumerable)ObjectValue).Cast<
object>().Select(x => (
ObjectReference)Reference.CreateReference(GetValue(x), elementType, GetKey(x)))
85 : ((IEnumerable)ObjectValue).Cast<
object>().Select((x, i) => (
ObjectReference)Reference.CreateReference(x, elementType, i)));
87 foreach (var reference
in references)
89 indices.Add(reference.Index);
97 foreach (var reference
in references)
99 if (reference.UpdateTarget(modelContainer))
109 return references.GetEnumerator();
113 IEnumerator IEnumerable.GetEnumerator()
115 return references.GetEnumerator();
122 return otherEnumerable != null && DesignExtensions.Equals<
IReference>(references, otherEnumerable.references);
128 string text =
"(" + references.Count +
" references";
129 if (references.Count > 0)
132 text += string.Join(
", ", references);
138 private static object GetKey(
object keyValuePair)
140 var type = keyValuePair.GetType();
141 if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(KeyValuePair<,>))
throw new ArgumentException(
"The given object is not a KeyValuePair.");
142 var keyProperty = type.GetProperty(
"Key");
143 return keyProperty.GetValue(keyValuePair);
146 private static object GetValue(
object keyValuePair)
148 var type = keyValuePair.GetType();
149 if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(KeyValuePair<,>))
throw new ArgumentException(
"The given object is not a KeyValuePair.");
150 var valueProperty = type.GetProperty(
"Value");
151 return valueProperty.GetValue(keyValuePair);
void Refresh(object newObjectValue)
Refresh this reference and its nested references.
A container used to store models and resolve references between them.
void Clear()
Clear the reference, making it represent a null or empty object.
A class representing an enumeration of references to multiple objects.
bool Equals(IReference other)
bool ContainsIndex(object index)
Indicates whether this instance of ReferenceEnumerable contains an element which as the given index...
bool UpdateTarget(ModelContainer modelContainer)
Updates the target node of this reference or its nested references from a ModelContainer. A ModelContainer in which the corresponding model node may have been registered.true if the model node was found in the ModelContainer and the target nodes has been updated, false otherwise.
override string ToString()
IEnumerator< ObjectReference > GetEnumerator()
A class representing a reference to another object that has a different model.