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

CustomAttributeData.cs « System.Reflection « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 069db8bf86ab94804b2357626686d60c9fe9d1ab (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//
// System.Reflection/CustomAttributeData.cs
//
// Author:
//   Zoltan Varga (vargaz@gmail.com)
//   Carlos Alberto Cortez (calberto.cortez@gmail.com)
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

namespace System.Reflection {

	[ComVisible (true)]
	[Serializable]
	public
	class CustomAttributeData {
		class LazyCAttrData {
			internal Assembly assembly;
			internal IntPtr data;
			internal uint data_length;
		}

		ConstructorInfo ctorInfo;
		IList<CustomAttributeTypedArgument> ctorArgs;
		IList<CustomAttributeNamedArgument> namedArgs;
		LazyCAttrData lazyData;


		protected CustomAttributeData ()
		{
		}

		// custom-attrs.c:create_custom_attr_data ()
		internal CustomAttributeData (ConstructorInfo ctorInfo, Assembly assembly, IntPtr data, uint data_length)
		{
			this.ctorInfo = ctorInfo;
			this.lazyData = new LazyCAttrData ();
			this.lazyData.assembly = assembly;
			this.lazyData.data = data;
			this.lazyData.data_length = data_length;
		}

		internal CustomAttributeData (ConstructorInfo ctorInfo)
			: this (ctorInfo, Array.Empty<CustomAttributeTypedArgument> (), Array.Empty<CustomAttributeNamedArgument> ())
		{
		}

		internal CustomAttributeData (ConstructorInfo ctorInfo, IList<CustomAttributeTypedArgument> ctorArgs, IList<CustomAttributeNamedArgument> namedArgs)
		{
			this.ctorInfo = ctorInfo;
			this.ctorArgs = ctorArgs;
			this.namedArgs = namedArgs;
		}

		[MethodImplAttribute (MethodImplOptions.InternalCall)]
		static extern void ResolveArgumentsInternal (ConstructorInfo ctor, Assembly assembly, IntPtr data, uint data_length, out object[] ctorArgs, out object[] namedArgs); 

		void ResolveArguments ()
		{
			object[] ctor_args, named_args;
			if (lazyData == null)
				return;

			ResolveArgumentsInternal (ctorInfo, lazyData.assembly, lazyData.data, lazyData.data_length, out ctor_args, out named_args);

			this.ctorArgs = Array.AsReadOnly<CustomAttributeTypedArgument>
				(ctor_args != null ? UnboxValues<CustomAttributeTypedArgument> (ctor_args) : Array.Empty<CustomAttributeTypedArgument>());
			this.namedArgs = Array.AsReadOnly<CustomAttributeNamedArgument> 
				(named_args != null ? UnboxValues<CustomAttributeNamedArgument> (named_args) : Array.Empty<CustomAttributeNamedArgument>());
			
			lazyData = null;
		}
		
		[ComVisible (true)]
		public
		virtual
		ConstructorInfo Constructor {
			get {
				return ctorInfo;
			}
		}

		[ComVisible (true)]
		public
		virtual
		IList<CustomAttributeTypedArgument> ConstructorArguments {
			get {
				ResolveArguments ();
				return ctorArgs;
			}
		}

		public
		virtual
		IList<CustomAttributeNamedArgument> NamedArguments {
			get {
				ResolveArguments ();
				return namedArgs;
			}
		}

		public static IList<CustomAttributeData> GetCustomAttributes (Assembly target) {
			return MonoCustomAttrs.GetCustomAttributesData (target);
		}

		public static IList<CustomAttributeData> GetCustomAttributes (MemberInfo target) {
			return MonoCustomAttrs.GetCustomAttributesData (target);
		}

		internal static IList<CustomAttributeData> GetCustomAttributesInternal (RuntimeType target) {
			return MonoCustomAttrs.GetCustomAttributesData (target);
		}

		public static IList<CustomAttributeData> GetCustomAttributes (Module target) {
			return MonoCustomAttrs.GetCustomAttributesData (target);
		}

		public static IList<CustomAttributeData> GetCustomAttributes (ParameterInfo target) {
			return MonoCustomAttrs.GetCustomAttributesData (target);
		}

#if NETCORE
		virtual
#endif
		public Type AttributeType {
			get { return ctorInfo.DeclaringType; }
		}

		public override string ToString ()
		{
			ResolveArguments ();

			StringBuilder sb = new StringBuilder ();

			sb.Append ("[" + ctorInfo.DeclaringType.FullName + "(");
			for (int i = 0; i < ctorArgs.Count; i++) {
				sb.Append (ctorArgs [i].ToString ());
				if (i + 1 < ctorArgs.Count)
					sb.Append (", ");
			}

			if (namedArgs.Count > 0)
				sb.Append (", ");
			
			for (int j = 0; j < namedArgs.Count; j++) {
				sb.Append (namedArgs [j].ToString ());
				if (j + 1 < namedArgs.Count)
					sb.Append (", ");
			}
			sb.AppendFormat (")]");

			return sb.ToString ();
		}

		static T [] UnboxValues<T> (object [] values)
		{
			T [] retval = new T [values.Length];
			for (int i = 0; i < values.Length; i++)
				retval [i] = (T) values [i];

			return retval;
		}

		public override bool Equals (object obj)
		{
#if NETCORE
			return obj == (object)this;
#else
			CustomAttributeData other = obj as CustomAttributeData;
			if (other == null || other.ctorInfo != ctorInfo ||
			    other.ctorArgs.Count != ctorArgs.Count ||
			    other.namedArgs.Count != namedArgs.Count)
				return false;
			for (int i = 0; i < ctorArgs.Count; i++)
				if (ctorArgs [i].Equals (other.ctorArgs [i]))
					return false;
			for (int i = 0; i < namedArgs.Count; i++) {
				bool matched = false;
				for (int j = 0; j < other.namedArgs.Count; j++)
					if (namedArgs [i].Equals (other.namedArgs [j])) {
						matched = true;
						break;
					}
				if (!matched)
					return false;
			}
			return true;
#endif
		}

		public override int GetHashCode ()
		{
#if NETCORE
			return base.GetHashCode ();
#else
			int ret = ctorInfo == null ? 13 : (ctorInfo.GetHashCode () << 16);
			// argument order-dependent
			if (ctorArgs != null) {
				for (int i = 0; i < ctorArgs.Count; i++) {
					ret += ret ^ 7 + ctorArgs [i].GetHashCode () << (i * 4);
				}
			}
			// argument order-independent
			if (namedArgs != null) {
				for (int i = 0; i < namedArgs.Count; i++)
					ret += (namedArgs [i].GetHashCode () << 5);
			}
			return ret;
#endif
		}
	}

}