Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ReflectionObjectEditor.cs « Reflection « Xamarin.PropertyEditing - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9616141d7978f61076517781d14434a92ad9834f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Linq;
using System.Threading.Tasks;

namespace Xamarin.PropertyEditing.Reflection
{
	public class ReflectionObjectEditor
		: IObjectEditor, INameableObject, IObjectEventEditor
	{
		public ReflectionObjectEditor (object target)
		{
			if (target == null)
				throw new ArgumentNullException (nameof (target));

			this.target = target;
			Type targetType = target.GetType ();

			this.properties.AddRange (ReflectionEditorProvider.GetPropertiesForType (targetType));

			foreach (EventInfo ev in targetType.GetEvents ()) {
				this.events.Add (new ReflectionEventInfo (ev));
			}
		}

		public event EventHandler<EditorPropertyChangedEventArgs> PropertyChanged;

		public object Target => this.target;

		public ITypeInfo TargetType => Target.GetType ().ToTypeInfo ();

		public IReadOnlyCollection<IPropertyInfo> Properties => this.properties;

		public IReadOnlyDictionary<IPropertyInfo, KnownProperty> KnownProperties => null;

		public IReadOnlyCollection<IEventInfo> Events => this.events;

		public IObjectEditor Parent => null;

		public IReadOnlyList<IObjectEditor> DirectChildren => EmptyDirectChildren;

		public string Name
		{
			get;
			private set;
		}

		public Task SetNameAsync (string name)
		{
			Name = name;
			return Task.FromResult (true);
		}

		public Task<string> GetNameAsync()
		{
			return Task.FromResult (Name);
		}

		public Task AttachHandlerAsync (IEventInfo ev, string handlerName)
		{
			throw new NotSupportedException ();
		}

		public Task DetachHandlerAsync (IEventInfo ev, string handlerName)
		{
			throw new NotSupportedException ();
		}

		public Task<IReadOnlyList<string>> GetHandlersAsync (IEventInfo ev)
		{
			if (ev == null)
				throw new ArgumentNullException (nameof (ev));

			ReflectionEventInfo info = ev as ReflectionEventInfo;
			if (info == null)
				throw new ArgumentException ();

			return Task.FromResult (info.GetHandlers (this.target));
		}

		public Task<AssignableTypesResult> GetAssignableTypesAsync (IPropertyInfo property, bool childTypes)
		{
			return GetAssignableTypes (property.RealType, childTypes);
		}

		public async Task SetValueAsync<T> (IPropertyInfo property, ValueInfo<T> value, PropertyVariation variation = null)
		{
			if (property == null)
				throw new ArgumentNullException (nameof (property));
			
			ReflectionPropertyInfo info = property as ReflectionPropertyInfo;
			if (info == null)
				throw new ArgumentException();

			await info.SetValueAsync (this.target, value.Value);
			OnPropertyChanged (info);
		}

		public Task<ITypeInfo> GetValueTypeAsync (IPropertyInfo property, PropertyVariation variation = null)
		{
			if (property == null)
				throw new ArgumentNullException (nameof (property));
			
			ReflectionPropertyInfo info = property as ReflectionPropertyInfo;
			if (info == null)
				throw new ArgumentException();

			return Task.FromResult (info.GetValueType (Target));
		}

		public async Task<ValueInfo<T>> GetValueAsync<T> (IPropertyInfo property, PropertyVariation variation = null)
		{
			if (property == null)
				throw new ArgumentNullException (nameof (property));
			
			ReflectionPropertyInfo info = property as ReflectionPropertyInfo;
			if (info == null)
				throw new ArgumentException();

			T value = await info.GetValueAsync<T> (this.target);

			return new ValueInfo<T> {
				Source = ValueSource.Local,
				Value = value
			};
		}

		internal static Task<AssignableTypesResult> GetAssignableTypes (ITypeInfo type, bool childTypes)
		{
			return Task.Run (() => {
				var types = AppDomain.CurrentDomain.GetAssemblies ().SelectMany (a => a.GetTypes ()).AsParallel ()
					.Where (t => t.Namespace != null && !t.IsAbstract && !t.IsInterface && t.IsPublic && t.GetConstructor (Type.EmptyTypes) != null);

				Type realType = ReflectionEditorProvider.GetRealType (type);
				if (childTypes) {
					var generic = realType.GetInterface ("ICollection`1");
					if (generic != null) {
						realType = generic.GetGenericArguments()[0];
					} else {
						realType = typeof(object);
					}
				}

				types = types.Where (t => realType.IsAssignableFrom (t));

				return new AssignableTypesResult (types.Select (t => {
					string asmName = t.Assembly.GetName ().Name;
					return new TypeInfo (new AssemblyInfo (asmName, isRelevant: asmName.StartsWith ("Xamarin")), t.Namespace, t.Name);
				}).ToList ());
			});
		}

		private readonly object target;
		private readonly List<ReflectionPropertyInfo> properties = new List<ReflectionPropertyInfo> ();
		private readonly List<ReflectionEventInfo> events = new List<ReflectionEventInfo> ();

		private static readonly IObjectEditor[] EmptyDirectChildren = new IObjectEditor[0];

		protected virtual void OnPropertyChanged (IPropertyInfo property)
		{
			PropertyChanged?.Invoke (this, new EditorPropertyChangedEventArgs (property));
		}
	}
}