1 | // Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using System.Collections.Generic; |
---|
21 | using System.Linq; |
---|
22 | using ICSharpCode.NRefactory.TypeSystem.Implementation; |
---|
23 | using ICSharpCode.NRefactory.Utils; |
---|
24 | |
---|
25 | namespace ICSharpCode.NRefactory.TypeSystem |
---|
26 | { |
---|
27 | /// <summary> |
---|
28 | /// Anonymous type. |
---|
29 | /// </summary> |
---|
30 | public class AnonymousType : AbstractType |
---|
31 | { |
---|
32 | ICompilation compilation; |
---|
33 | IUnresolvedProperty[] unresolvedProperties; |
---|
34 | IList<IProperty> resolvedProperties; |
---|
35 | |
---|
36 | public AnonymousType(ICompilation compilation, IList<IUnresolvedProperty> properties) |
---|
37 | { |
---|
38 | if (compilation == null) |
---|
39 | throw new ArgumentNullException("compilation"); |
---|
40 | if (properties == null) |
---|
41 | throw new ArgumentNullException("properties"); |
---|
42 | this.compilation = compilation; |
---|
43 | this.unresolvedProperties = properties.ToArray(); |
---|
44 | var context = new SimpleTypeResolveContext(compilation.MainAssembly); |
---|
45 | this.resolvedProperties = new ProjectedList<ITypeResolveContext, IUnresolvedProperty, IProperty>(context, unresolvedProperties, (c, p) => new AnonymousTypeProperty(p, c, this)); |
---|
46 | } |
---|
47 | |
---|
48 | sealed class AnonymousTypeProperty : DefaultResolvedProperty |
---|
49 | { |
---|
50 | readonly AnonymousType declaringType; |
---|
51 | |
---|
52 | public AnonymousTypeProperty(IUnresolvedProperty unresolved, ITypeResolveContext parentContext, AnonymousType declaringType) |
---|
53 | : base(unresolved, parentContext) |
---|
54 | { |
---|
55 | this.declaringType = declaringType; |
---|
56 | } |
---|
57 | |
---|
58 | public override IType DeclaringType { |
---|
59 | get { return declaringType; } |
---|
60 | } |
---|
61 | |
---|
62 | public override bool Equals(object obj) |
---|
63 | { |
---|
64 | AnonymousTypeProperty p = obj as AnonymousTypeProperty; |
---|
65 | return p != null && this.Name == p.Name && declaringType.Equals(p.declaringType); |
---|
66 | } |
---|
67 | |
---|
68 | public override int GetHashCode() |
---|
69 | { |
---|
70 | return declaringType.GetHashCode() ^ unchecked(27 * this.Name.GetHashCode()); |
---|
71 | } |
---|
72 | |
---|
73 | protected override IMethod CreateResolvedAccessor(IUnresolvedMethod unresolvedAccessor) |
---|
74 | { |
---|
75 | return new AnonymousTypeAccessor(unresolvedAccessor, context, this); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | sealed class AnonymousTypeAccessor : DefaultResolvedMethod |
---|
80 | { |
---|
81 | readonly AnonymousTypeProperty owner; |
---|
82 | |
---|
83 | public AnonymousTypeAccessor(IUnresolvedMethod unresolved, ITypeResolveContext parentContext, AnonymousTypeProperty owner) |
---|
84 | : base(unresolved, parentContext, isExtensionMethod: false) |
---|
85 | { |
---|
86 | this.owner = owner; |
---|
87 | } |
---|
88 | |
---|
89 | public override IMember AccessorOwner { |
---|
90 | get { return owner; } |
---|
91 | } |
---|
92 | |
---|
93 | public override IType DeclaringType { |
---|
94 | get { return owner.DeclaringType; } |
---|
95 | } |
---|
96 | |
---|
97 | public override bool Equals(object obj) |
---|
98 | { |
---|
99 | AnonymousTypeAccessor p = obj as AnonymousTypeAccessor; |
---|
100 | return p != null && this.Name == p.Name && owner.DeclaringType.Equals(p.owner.DeclaringType); |
---|
101 | } |
---|
102 | |
---|
103 | public override int GetHashCode() |
---|
104 | { |
---|
105 | return owner.DeclaringType.GetHashCode() ^ unchecked(27 * this.Name.GetHashCode()); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | public override ITypeReference ToTypeReference() |
---|
110 | { |
---|
111 | return new AnonymousTypeReference(unresolvedProperties); |
---|
112 | } |
---|
113 | |
---|
114 | public override string Name { |
---|
115 | get { return "Anonymous Type"; } |
---|
116 | } |
---|
117 | |
---|
118 | public override TypeKind Kind { |
---|
119 | get { return TypeKind.Anonymous; } |
---|
120 | } |
---|
121 | |
---|
122 | public override IEnumerable<IType> DirectBaseTypes { |
---|
123 | get { |
---|
124 | yield return compilation.FindType(KnownTypeCode.Object); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | public override bool? IsReferenceType { |
---|
129 | get { return true; } |
---|
130 | } |
---|
131 | |
---|
132 | public IList<IProperty> Properties { |
---|
133 | get { return resolvedProperties; } |
---|
134 | } |
---|
135 | |
---|
136 | public override IEnumerable<IMethod> GetMethods(Predicate<IUnresolvedMethod> filter = null, GetMemberOptions options = GetMemberOptions.None) |
---|
137 | { |
---|
138 | if ((options & GetMemberOptions.IgnoreInheritedMembers) == GetMemberOptions.IgnoreInheritedMembers) |
---|
139 | return EmptyList<IMethod>.Instance; |
---|
140 | else |
---|
141 | return compilation.FindType(KnownTypeCode.Object).GetMethods(filter, options); |
---|
142 | } |
---|
143 | |
---|
144 | public override IEnumerable<IMethod> GetMethods(IList<IType> typeArguments, Predicate<IUnresolvedMethod> filter = null, GetMemberOptions options = GetMemberOptions.None) |
---|
145 | { |
---|
146 | if ((options & GetMemberOptions.IgnoreInheritedMembers) == GetMemberOptions.IgnoreInheritedMembers) |
---|
147 | return EmptyList<IMethod>.Instance; |
---|
148 | else |
---|
149 | return compilation.FindType(KnownTypeCode.Object).GetMethods(typeArguments, filter, options); |
---|
150 | } |
---|
151 | |
---|
152 | public override IEnumerable<IProperty> GetProperties(Predicate<IUnresolvedProperty> filter = null, GetMemberOptions options = GetMemberOptions.None) |
---|
153 | { |
---|
154 | for (int i = 0; i < unresolvedProperties.Length; i++) { |
---|
155 | if (filter == null || filter(unresolvedProperties[i])) |
---|
156 | yield return resolvedProperties[i]; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | public override IEnumerable<IMethod> GetAccessors(Predicate<IUnresolvedMethod> filter, GetMemberOptions options) |
---|
161 | { |
---|
162 | for (int i = 0; i < unresolvedProperties.Length; i++) { |
---|
163 | if (unresolvedProperties[i].CanGet) { |
---|
164 | if (filter == null || filter(unresolvedProperties[i].Getter)) |
---|
165 | yield return resolvedProperties[i].Getter; |
---|
166 | } |
---|
167 | if (unresolvedProperties[i].CanSet) { |
---|
168 | if (filter == null || filter(unresolvedProperties[i].Setter)) |
---|
169 | yield return resolvedProperties[i].Setter; |
---|
170 | } |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | public override int GetHashCode() |
---|
175 | { |
---|
176 | unchecked { |
---|
177 | int hashCode = resolvedProperties.Count; |
---|
178 | foreach (var p in resolvedProperties) { |
---|
179 | hashCode *= 31; |
---|
180 | hashCode += p.Name.GetHashCode() ^ p.ReturnType.GetHashCode(); |
---|
181 | } |
---|
182 | return hashCode; |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | public override bool Equals(IType other) |
---|
187 | { |
---|
188 | AnonymousType o = other as AnonymousType; |
---|
189 | if (o == null || resolvedProperties.Count != o.resolvedProperties.Count) |
---|
190 | return false; |
---|
191 | for (int i = 0; i < resolvedProperties.Count; i++) { |
---|
192 | IProperty p1 = resolvedProperties[i]; |
---|
193 | IProperty p2 = o.resolvedProperties[i]; |
---|
194 | if (p1.Name != p2.Name || !p1.ReturnType.Equals(p2.ReturnType)) |
---|
195 | return false; |
---|
196 | } |
---|
197 | return true; |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | /// <summary> |
---|
202 | /// Anonymous type reference. |
---|
203 | /// </summary> |
---|
204 | [Serializable] |
---|
205 | public class AnonymousTypeReference : ITypeReference |
---|
206 | { |
---|
207 | readonly IUnresolvedProperty[] unresolvedProperties; |
---|
208 | |
---|
209 | public AnonymousTypeReference(IUnresolvedProperty[] properties) |
---|
210 | { |
---|
211 | if (properties == null) |
---|
212 | throw new ArgumentNullException("properties"); |
---|
213 | this.unresolvedProperties = properties; |
---|
214 | } |
---|
215 | |
---|
216 | public IType Resolve(ITypeResolveContext context) |
---|
217 | { |
---|
218 | return new AnonymousType(context.Compilation, unresolvedProperties); |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|