MetaType.cs 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  1. #if !NO_RUNTIME
  2. using System;
  3. using System.Collections;
  4. using System.Text;
  5. using ProtoBuf.Serializers;
  6. #if FEAT_IKVM
  7. using Type = IKVM.Reflection.Type;
  8. using IKVM.Reflection;
  9. #if FEAT_COMPILER
  10. using IKVM.Reflection.Emit;
  11. #endif
  12. #else
  13. using System.Reflection;
  14. #if FEAT_COMPILER
  15. using System.Reflection.Emit;
  16. #endif
  17. #endif
  18. namespace ProtoBuf.Meta
  19. {
  20. /// <summary>
  21. /// Represents a type at runtime for use with protobuf, allowing the field mappings (etc) to be defined
  22. /// </summary>
  23. public class MetaType : ISerializerProxy
  24. {
  25. internal sealed class Comparer : IComparer
  26. #if !NO_GENERICS
  27. , System.Collections.Generic.IComparer<MetaType>
  28. #endif
  29. {
  30. public static readonly Comparer Default = new Comparer();
  31. public int Compare(object x, object y)
  32. {
  33. return Compare(x as MetaType, y as MetaType);
  34. }
  35. public int Compare(MetaType x, MetaType y)
  36. {
  37. if (ReferenceEquals(x, y)) return 0;
  38. if (x == null) return -1;
  39. if (y == null) return 1;
  40. #if FX11
  41. return string.Compare(x.GetSchemaTypeName(), y.GetSchemaTypeName());
  42. #else
  43. return string.Compare(x.GetSchemaTypeName(), y.GetSchemaTypeName(), StringComparison.Ordinal);
  44. #endif
  45. }
  46. }
  47. /// <summary>
  48. /// Get the name of the type being represented
  49. /// </summary>
  50. public override string ToString()
  51. {
  52. return type.ToString();
  53. }
  54. IProtoSerializer ISerializerProxy.Serializer { get { return Serializer; } }
  55. private MetaType baseType;
  56. /// <summary>
  57. /// Gets the base-type for this type
  58. /// </summary>
  59. public MetaType BaseType {
  60. get { return baseType; }
  61. }
  62. internal TypeModel Model { get { return model; } }
  63. /// <summary>
  64. /// When used to compile a model, should public serialization/deserialzation methods
  65. /// be included for this type?
  66. /// </summary>
  67. public bool IncludeSerializerMethod
  68. { // negated to minimize common-case / initializer
  69. get { return !HasFlag(OPTIONS_PrivateOnApi); }
  70. set { SetFlag(OPTIONS_PrivateOnApi, !value, true); }
  71. }
  72. /// <summary>
  73. /// Should this type be treated as a reference by default?
  74. /// </summary>
  75. public bool AsReferenceDefault
  76. {
  77. get { return HasFlag(OPTIONS_AsReferenceDefault); }
  78. set { SetFlag(OPTIONS_AsReferenceDefault, value, true); }
  79. }
  80. private BasicList subTypes;
  81. private bool IsValidSubType(Type subType)
  82. {
  83. #if WINRT || COREFX
  84. return typeInfo.IsAssignableFrom(subType.GetTypeInfo());
  85. #else
  86. return type.IsAssignableFrom(subType);
  87. #endif
  88. }
  89. /// <summary>
  90. /// Adds a known sub-type to the inheritance model
  91. /// </summary>
  92. public MetaType AddSubType(int fieldNumber, Type derivedType)
  93. {
  94. return AddSubType(fieldNumber, derivedType, DataFormat.Default);
  95. }
  96. /// <summary>
  97. /// Adds a known sub-type to the inheritance model
  98. /// </summary>
  99. public MetaType AddSubType(int fieldNumber, Type derivedType, DataFormat dataFormat)
  100. {
  101. if (derivedType == null) throw new ArgumentNullException("derivedType");
  102. if (fieldNumber < 1) throw new ArgumentOutOfRangeException("fieldNumber");
  103. #if WINRT || COREFX || COREFX
  104. if (!(typeInfo.IsClass || typeInfo.IsInterface) || typeInfo.IsSealed) {
  105. #else
  106. if (!(type.IsClass || type.IsInterface) || type.IsSealed) {
  107. #endif
  108. throw new InvalidOperationException("Sub-types can only be added to non-sealed classes");
  109. }
  110. if (!IsValidSubType(derivedType))
  111. {
  112. throw new ArgumentException(derivedType.Name + " is not a valid sub-type of " + type.Name, "derivedType");
  113. }
  114. MetaType derivedMeta = model[derivedType];
  115. ThrowIfFrozen();
  116. derivedMeta.ThrowIfFrozen();
  117. SubType subType = new SubType(fieldNumber, derivedMeta, dataFormat);
  118. ThrowIfFrozen();
  119. derivedMeta.SetBaseType(this); // includes ThrowIfFrozen
  120. if (subTypes == null) subTypes = new BasicList();
  121. subTypes.Add(subType);
  122. return this;
  123. }
  124. #if WINRT || COREFX
  125. internal static readonly TypeInfo ienumerable = typeof(IEnumerable).GetTypeInfo();
  126. #else
  127. internal static readonly System.Type ienumerable = typeof(IEnumerable);
  128. #endif
  129. private void SetBaseType(MetaType baseType)
  130. {
  131. if (baseType == null) throw new ArgumentNullException("baseType");
  132. if (this.baseType == baseType) return;
  133. if (this.baseType != null) throw new InvalidOperationException("A type can only participate in one inheritance hierarchy");
  134. MetaType type = baseType;
  135. while (type != null)
  136. {
  137. if (ReferenceEquals(type, this)) throw new InvalidOperationException("Cyclic inheritance is not allowed");
  138. type = type.baseType;
  139. }
  140. this.baseType = baseType;
  141. }
  142. private CallbackSet callbacks;
  143. /// <summary>
  144. /// Indicates whether the current type has defined callbacks
  145. /// </summary>
  146. public bool HasCallbacks
  147. {
  148. get { return callbacks != null && callbacks.NonTrivial; }
  149. }
  150. /// <summary>
  151. /// Indicates whether the current type has defined subtypes
  152. /// </summary>
  153. public bool HasSubtypes
  154. {
  155. get { return subTypes != null && subTypes.Count != 0; }
  156. }
  157. /// <summary>
  158. /// Returns the set of callbacks defined for this type
  159. /// </summary>
  160. public CallbackSet Callbacks
  161. {
  162. get
  163. {
  164. if (callbacks == null) callbacks = new CallbackSet(this);
  165. return callbacks;
  166. }
  167. }
  168. private bool IsValueType
  169. {
  170. get
  171. {
  172. #if WINRT || COREFX
  173. return typeInfo.IsValueType;
  174. #else
  175. return type.IsValueType;
  176. #endif
  177. }
  178. }
  179. /// <summary>
  180. /// Assigns the callbacks to use during serialiation/deserialization.
  181. /// </summary>
  182. /// <param name="beforeSerialize">The method (or null) called before serialization begins.</param>
  183. /// <param name="afterSerialize">The method (or null) called when serialization is complete.</param>
  184. /// <param name="beforeDeserialize">The method (or null) called before deserialization begins (or when a new instance is created during deserialization).</param>
  185. /// <param name="afterDeserialize">The method (or null) called when deserialization is complete.</param>
  186. /// <returns>The set of callbacks.</returns>
  187. public MetaType SetCallbacks(MethodInfo beforeSerialize, MethodInfo afterSerialize, MethodInfo beforeDeserialize, MethodInfo afterDeserialize)
  188. {
  189. CallbackSet callbacks = Callbacks;
  190. callbacks.BeforeSerialize = beforeSerialize;
  191. callbacks.AfterSerialize = afterSerialize;
  192. callbacks.BeforeDeserialize = beforeDeserialize;
  193. callbacks.AfterDeserialize = afterDeserialize;
  194. return this;
  195. }
  196. /// <summary>
  197. /// Assigns the callbacks to use during serialiation/deserialization.
  198. /// </summary>
  199. /// <param name="beforeSerialize">The name of the method (or null) called before serialization begins.</param>
  200. /// <param name="afterSerialize">The name of the method (or null) called when serialization is complete.</param>
  201. /// <param name="beforeDeserialize">The name of the method (or null) called before deserialization begins (or when a new instance is created during deserialization).</param>
  202. /// <param name="afterDeserialize">The name of the method (or null) called when deserialization is complete.</param>
  203. /// <returns>The set of callbacks.</returns>
  204. public MetaType SetCallbacks(string beforeSerialize, string afterSerialize, string beforeDeserialize, string afterDeserialize)
  205. {
  206. if (IsValueType) throw new InvalidOperationException();
  207. CallbackSet callbacks = Callbacks;
  208. callbacks.BeforeSerialize = ResolveMethod(beforeSerialize, true);
  209. callbacks.AfterSerialize = ResolveMethod(afterSerialize, true);
  210. callbacks.BeforeDeserialize = ResolveMethod(beforeDeserialize, true);
  211. callbacks.AfterDeserialize = ResolveMethod(afterDeserialize, true);
  212. return this;
  213. }
  214. internal string GetSchemaTypeName()
  215. {
  216. if (surrogate != null) return model[surrogate].GetSchemaTypeName();
  217. if (!Helpers.IsNullOrEmpty(name)) return name;
  218. string typeName = type.Name;
  219. #if !NO_GENERICS
  220. if (type
  221. #if WINRT || COREFX
  222. .GetTypeInfo()
  223. #endif
  224. .IsGenericType)
  225. {
  226. StringBuilder sb = new StringBuilder(typeName);
  227. int split = typeName.IndexOf('`');
  228. if (split >= 0) sb.Length = split;
  229. foreach (Type arg in type
  230. #if WINRT || COREFX
  231. .GetTypeInfo().GenericTypeArguments
  232. #else
  233. .GetGenericArguments()
  234. #endif
  235. )
  236. {
  237. sb.Append('_');
  238. Type tmp = arg;
  239. int key = model.GetKey(ref tmp);
  240. MetaType mt;
  241. if (key >= 0 && (mt = model[tmp]) != null && mt.surrogate == null) // <=== need to exclude surrogate to avoid chance of infinite loop
  242. {
  243. sb.Append(mt.GetSchemaTypeName());
  244. }
  245. else
  246. {
  247. sb.Append(tmp.Name);
  248. }
  249. }
  250. return sb.ToString();
  251. }
  252. #endif
  253. return typeName;
  254. }
  255. private string name;
  256. /// <summary>
  257. /// Gets or sets the name of this contract.
  258. /// </summary>
  259. public string Name
  260. {
  261. get
  262. {
  263. return name;
  264. }
  265. set
  266. {
  267. ThrowIfFrozen();
  268. name = value;
  269. }
  270. }
  271. private MethodInfo factory;
  272. /// <summary>
  273. /// Designate a factory-method to use to create instances of this type
  274. /// </summary>
  275. public MetaType SetFactory(MethodInfo factory)
  276. {
  277. model.VerifyFactory(factory, type);
  278. ThrowIfFrozen();
  279. this.factory = factory;
  280. return this;
  281. }
  282. /// <summary>
  283. /// Designate a factory-method to use to create instances of this type
  284. /// </summary>
  285. public MetaType SetFactory(string factory)
  286. {
  287. return SetFactory(ResolveMethod(factory, false));
  288. }
  289. private MethodInfo ResolveMethod(string name, bool instance)
  290. {
  291. if (Helpers.IsNullOrEmpty(name)) return null;
  292. #if WINRT || COREFX
  293. return instance ? Helpers.GetInstanceMethod(typeInfo, name) : Helpers.GetStaticMethod(typeInfo, name);
  294. #else
  295. return instance ? Helpers.GetInstanceMethod(type, name) : Helpers.GetStaticMethod(type, name);
  296. #endif
  297. }
  298. private readonly RuntimeTypeModel model;
  299. internal static Exception InbuiltType(Type type)
  300. {
  301. return new ArgumentException("Data of this type has inbuilt behaviour, and cannot be added to a model in this way: " + type.FullName);
  302. }
  303. internal MetaType(RuntimeTypeModel model, Type type, MethodInfo factory)
  304. {
  305. this.factory = factory;
  306. if (model == null) throw new ArgumentNullException("model");
  307. if (type == null) throw new ArgumentNullException("type");
  308. IProtoSerializer coreSerializer = model.TryGetBasicTypeSerializer(type);
  309. if (coreSerializer != null)
  310. {
  311. throw InbuiltType(type);
  312. }
  313. this.type = type;
  314. #if WINRT || COREFX
  315. this.typeInfo = type.GetTypeInfo();
  316. #endif
  317. this.model = model;
  318. if (Helpers.IsEnum(type))
  319. {
  320. #if WINRT || COREFX
  321. EnumPassthru = typeInfo.IsDefined(typeof(FlagsAttribute), false);
  322. #else
  323. EnumPassthru = type.IsDefined(model.MapType(typeof(FlagsAttribute)), false);
  324. #endif
  325. }
  326. }
  327. #if WINRT || COREFX
  328. private readonly TypeInfo typeInfo;
  329. #endif
  330. /// <summary>
  331. /// Throws an exception if the type has been made immutable
  332. /// </summary>
  333. protected internal void ThrowIfFrozen()
  334. {
  335. if ((flags & OPTIONS_Frozen)!=0) throw new InvalidOperationException("The type cannot be changed once a serializer has been generated for " + type.FullName);
  336. }
  337. //internal void Freeze() { flags |= OPTIONS_Frozen; }
  338. private readonly Type type;
  339. /// <summary>
  340. /// The runtime type that the meta-type represents
  341. /// </summary>
  342. public Type Type { get { return type; } }
  343. private IProtoTypeSerializer serializer;
  344. internal IProtoTypeSerializer Serializer {
  345. get {
  346. if (serializer == null)
  347. {
  348. int opaqueToken = 0;
  349. try
  350. {
  351. model.TakeLock(ref opaqueToken);
  352. if (serializer == null)
  353. { // double-check, but our main purpse with this lock is to ensure thread-safety with
  354. // serializers needing to wait until another thread has finished adding the properties
  355. SetFlag(OPTIONS_Frozen, true, false);
  356. serializer = BuildSerializer();
  357. #if FEAT_COMPILER && !FX11
  358. if (model.AutoCompile) CompileInPlace();
  359. #endif
  360. }
  361. }
  362. finally
  363. {
  364. model.ReleaseLock(opaqueToken);
  365. }
  366. }
  367. return serializer;
  368. }
  369. }
  370. internal bool IsList
  371. {
  372. get
  373. {
  374. Type itemType = IgnoreListHandling ? null : TypeModel.GetListItemType(model, type);
  375. return itemType != null;
  376. }
  377. }
  378. private IProtoTypeSerializer BuildSerializer()
  379. {
  380. if (Helpers.IsEnum(type))
  381. {
  382. return new TagDecorator(ProtoBuf.Serializer.ListItemTag, WireType.Variant, false, new EnumSerializer(type, GetEnumMap()));
  383. }
  384. Type itemType = IgnoreListHandling ? null : TypeModel.GetListItemType(model, type);
  385. if (itemType != null)
  386. {
  387. if(surrogate != null)
  388. {
  389. throw new ArgumentException("Repeated data (a list, collection, etc) has inbuilt behaviour and cannot use a surrogate");
  390. }
  391. if(subTypes != null && subTypes.Count != 0)
  392. {
  393. throw new ArgumentException("Repeated data (a list, collection, etc) has inbuilt behaviour and cannot be subclassed");
  394. }
  395. Type defaultType = null;
  396. ResolveListTypes(model, type, ref itemType, ref defaultType);
  397. ValueMember fakeMember = new ValueMember(model, ProtoBuf.Serializer.ListItemTag, type, itemType, defaultType, DataFormat.Default);
  398. return new TypeSerializer(model, type, new int[] { ProtoBuf.Serializer.ListItemTag }, new IProtoSerializer[] { fakeMember.Serializer }, null, true, true, null, constructType, factory);
  399. }
  400. if (surrogate != null)
  401. {
  402. MetaType mt = model[surrogate], mtBase;
  403. while ((mtBase = mt.baseType) != null) { mt = mtBase; }
  404. return new SurrogateSerializer(model, type, surrogate, mt.Serializer);
  405. }
  406. if (IsAutoTuple)
  407. {
  408. MemberInfo[] mapping;
  409. ConstructorInfo ctor = ResolveTupleConstructor(type, out mapping);
  410. if(ctor == null) throw new InvalidOperationException();
  411. return new TupleSerializer(model, ctor, mapping);
  412. }
  413. fields.Trim();
  414. int fieldCount = fields.Count;
  415. int subTypeCount = subTypes == null ? 0 : subTypes.Count;
  416. int[] fieldNumbers = new int[fieldCount + subTypeCount];
  417. IProtoSerializer[] serializers = new IProtoSerializer[fieldCount + subTypeCount];
  418. int i = 0;
  419. if (subTypeCount != 0)
  420. {
  421. foreach (SubType subType in subTypes)
  422. {
  423. #if WINRT || COREFX
  424. if (!subType.DerivedType.IgnoreListHandling && ienumerable.IsAssignableFrom(subType.DerivedType.Type.GetTypeInfo()))
  425. #else
  426. if (!subType.DerivedType.IgnoreListHandling && model.MapType(ienumerable).IsAssignableFrom(subType.DerivedType.Type))
  427. #endif
  428. {
  429. throw new ArgumentException("Repeated data (a list, collection, etc) has inbuilt behaviour and cannot be used as a subclass");
  430. }
  431. fieldNumbers[i] = subType.FieldNumber;
  432. serializers[i++] = subType.Serializer;
  433. }
  434. }
  435. if (fieldCount != 0)
  436. {
  437. foreach (ValueMember member in fields)
  438. {
  439. fieldNumbers[i] = member.FieldNumber;
  440. serializers[i++] = member.Serializer;
  441. }
  442. }
  443. BasicList baseCtorCallbacks = null;
  444. MetaType tmp = BaseType;
  445. while (tmp != null)
  446. {
  447. MethodInfo method = tmp.HasCallbacks ? tmp.Callbacks.BeforeDeserialize : null;
  448. if (method != null)
  449. {
  450. if (baseCtorCallbacks == null) baseCtorCallbacks = new BasicList();
  451. baseCtorCallbacks.Add(method);
  452. }
  453. tmp = tmp.BaseType;
  454. }
  455. MethodInfo[] arr = null;
  456. if (baseCtorCallbacks != null)
  457. {
  458. arr = new MethodInfo[baseCtorCallbacks.Count];
  459. baseCtorCallbacks.CopyTo(arr, 0);
  460. Array.Reverse(arr);
  461. }
  462. return new TypeSerializer(model, type, fieldNumbers, serializers, arr, baseType == null, UseConstructor, callbacks, constructType, factory);
  463. }
  464. [Flags]
  465. internal enum AttributeFamily
  466. {
  467. None = 0, ProtoBuf = 1, DataContractSerialier = 2, XmlSerializer = 4, AutoTuple = 8
  468. }
  469. static Type GetBaseType(MetaType type)
  470. {
  471. #if WINRT || COREFX
  472. return type.typeInfo.BaseType;
  473. #else
  474. return type.type.BaseType;
  475. #endif
  476. }
  477. internal static bool GetAsReferenceDefault(RuntimeTypeModel model, Type type)
  478. {
  479. if (type == null) throw new ArgumentNullException("type");
  480. if (Helpers.IsEnum(type)) return false; // never as-ref
  481. AttributeMap[] typeAttribs = AttributeMap.Create(model, type, false);
  482. for (int i = 0; i < typeAttribs.Length; i++)
  483. {
  484. if (typeAttribs[i].AttributeType.FullName == "ProtoBuf.ProtoContractAttribute")
  485. {
  486. object tmp;
  487. if (typeAttribs[i].TryGet("AsReferenceDefault", out tmp)) return (bool)tmp;
  488. }
  489. }
  490. return false;
  491. }
  492. internal void ApplyDefaultBehaviour()
  493. {
  494. Type baseType = GetBaseType(this);
  495. if (baseType != null && model.FindWithoutAdd(baseType) == null
  496. && GetContractFamily(model, baseType, null) != MetaType.AttributeFamily.None)
  497. {
  498. model.FindOrAddAuto(baseType, true, false, false);
  499. }
  500. AttributeMap[] typeAttribs = AttributeMap.Create(model, type, false);
  501. AttributeFamily family = GetContractFamily(model, type, typeAttribs);
  502. if(family == AttributeFamily.AutoTuple)
  503. {
  504. SetFlag(OPTIONS_AutoTuple, true, true);
  505. }
  506. bool isEnum = !EnumPassthru && Helpers.IsEnum(type);
  507. if(family == AttributeFamily.None && !isEnum) return; // and you'd like me to do what, exactly?
  508. BasicList partialIgnores = null, partialMembers = null;
  509. int dataMemberOffset = 0, implicitFirstTag = 1;
  510. bool inferTagByName = model.InferTagFromNameDefault;
  511. ImplicitFields implicitMode = ImplicitFields.None;
  512. string name = null;
  513. for (int i = 0; i < typeAttribs.Length; i++)
  514. {
  515. AttributeMap item = (AttributeMap)typeAttribs[i];
  516. object tmp;
  517. string fullAttributeTypeName = item.AttributeType.FullName;
  518. if (!isEnum && fullAttributeTypeName == "ProtoBuf.ProtoIncludeAttribute")
  519. {
  520. int tag = 0;
  521. if (item.TryGet("tag", out tmp)) tag = (int)tmp;
  522. DataFormat dataFormat = DataFormat.Default;
  523. if(item.TryGet("DataFormat", out tmp))
  524. {
  525. dataFormat = (DataFormat)(int) tmp;
  526. }
  527. Type knownType = null;
  528. try
  529. {
  530. if (item.TryGet("knownTypeName", out tmp)) knownType = model.GetType((string)tmp, type
  531. #if WINRT || COREFX
  532. .GetTypeInfo()
  533. #endif
  534. .Assembly);
  535. else if (item.TryGet("knownType", out tmp)) knownType = (Type)tmp;
  536. }
  537. catch (Exception ex)
  538. {
  539. throw new InvalidOperationException("Unable to resolve sub-type of: " + type.FullName, ex);
  540. }
  541. if (knownType == null)
  542. {
  543. throw new InvalidOperationException("Unable to resolve sub-type of: " + type.FullName);
  544. }
  545. if(IsValidSubType(knownType)) AddSubType(tag, knownType, dataFormat);
  546. }
  547. if (fullAttributeTypeName == "ProtoBuf.ProtoPartialIgnoreAttribute")
  548. {
  549. if (item.TryGet("MemberName", out tmp) && tmp != null)
  550. {
  551. if (partialIgnores == null) partialIgnores = new BasicList();
  552. partialIgnores.Add((string)tmp);
  553. }
  554. }
  555. if (!isEnum && fullAttributeTypeName == "ProtoBuf.ProtoPartialMemberAttribute")
  556. {
  557. if (partialMembers == null) partialMembers = new BasicList();
  558. partialMembers.Add(item);
  559. }
  560. if (fullAttributeTypeName == "ProtoBuf.ProtoContractAttribute")
  561. {
  562. if (item.TryGet("Name", out tmp)) name = (string) tmp;
  563. if (Helpers.IsEnum(type)) // note this is subtly different to isEnum; want to do this even if [Flags]
  564. {
  565. #if !FEAT_IKVM
  566. // IKVM can't access EnumPassthruHasValue, but conveniently, InferTagFromName will only be returned if set via ctor or property
  567. if (item.TryGet("EnumPassthruHasValue", false, out tmp) && (bool)tmp)
  568. #endif
  569. {
  570. if (item.TryGet("EnumPassthru", out tmp))
  571. {
  572. EnumPassthru = (bool)tmp;
  573. if (EnumPassthru) isEnum = false; // no longer treated as an enum
  574. }
  575. }
  576. }
  577. else
  578. {
  579. if (item.TryGet("DataMemberOffset", out tmp)) dataMemberOffset = (int) tmp;
  580. #if !FEAT_IKVM
  581. // IKVM can't access InferTagFromNameHasValue, but conveniently, InferTagFromName will only be returned if set via ctor or property
  582. if (item.TryGet("InferTagFromNameHasValue", false, out tmp) && (bool) tmp)
  583. #endif
  584. {
  585. if (item.TryGet("InferTagFromName", out tmp)) inferTagByName = (bool) tmp;
  586. }
  587. if (item.TryGet("ImplicitFields", out tmp) && tmp != null)
  588. {
  589. implicitMode = (ImplicitFields) (int) tmp; // note that this uses the bizarre unboxing rules of enums/underlying-types
  590. }
  591. if (item.TryGet("SkipConstructor", out tmp)) UseConstructor = !(bool) tmp;
  592. if (item.TryGet("IgnoreListHandling", out tmp)) IgnoreListHandling = (bool) tmp;
  593. if (item.TryGet("AsReferenceDefault", out tmp)) AsReferenceDefault = (bool) tmp;
  594. if (item.TryGet("ImplicitFirstTag", out tmp) && (int) tmp > 0) implicitFirstTag = (int) tmp;
  595. }
  596. }
  597. if (fullAttributeTypeName == "System.Runtime.Serialization.DataContractAttribute")
  598. {
  599. if (name == null && item.TryGet("Name", out tmp)) name = (string)tmp;
  600. }
  601. if (fullAttributeTypeName == "System.Xml.Serialization.XmlTypeAttribute")
  602. {
  603. if (name == null && item.TryGet("TypeName", out tmp)) name = (string)tmp;
  604. }
  605. }
  606. if (!Helpers.IsNullOrEmpty(name)) Name = name;
  607. if (implicitMode != ImplicitFields.None)
  608. {
  609. family &= AttributeFamily.ProtoBuf; // with implicit fields, **only** proto attributes are important
  610. }
  611. MethodInfo[] callbacks = null;
  612. BasicList members = new BasicList();
  613. #if WINRT
  614. System.Collections.Generic.IEnumerable<MemberInfo> foundList;
  615. if(isEnum) {
  616. foundList = type.GetRuntimeFields();
  617. }
  618. else
  619. {
  620. System.Collections.Generic.List<MemberInfo> list = new System.Collections.Generic.List<MemberInfo>();
  621. foreach(PropertyInfo prop in type.GetRuntimeProperties()) {
  622. MethodInfo getter = Helpers.GetGetMethod(prop, false, false);
  623. if(getter != null && !getter.IsStatic) list.Add(prop);
  624. }
  625. foreach(FieldInfo fld in type.GetRuntimeFields()) if(fld.IsPublic && !fld.IsStatic) list.Add(fld);
  626. foreach(MethodInfo mthd in type.GetRuntimeMethods()) if(mthd.IsPublic && !mthd.IsStatic) list.Add(mthd);
  627. foundList = list;
  628. }
  629. #else
  630. MemberInfo[] foundList = type.GetMembers(isEnum ? BindingFlags.Public | BindingFlags.Static
  631. : BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  632. #endif
  633. foreach (MemberInfo member in foundList)
  634. {
  635. if (member.DeclaringType != type) continue;
  636. if (member.IsDefined(model.MapType(typeof(ProtoIgnoreAttribute)), true)) continue;
  637. if (partialIgnores != null && partialIgnores.Contains(member.Name)) continue;
  638. bool forced = false, isPublic, isField;
  639. Type effectiveType;
  640. PropertyInfo property;
  641. FieldInfo field;
  642. MethodInfo method;
  643. if((property = member as PropertyInfo) != null)
  644. {
  645. if (isEnum) continue; // wasn't expecting any props!
  646. effectiveType = property.PropertyType;
  647. isPublic = Helpers.GetGetMethod(property, false, false) != null;
  648. isField = false;
  649. ApplyDefaultBehaviour_AddMembers(model, family, isEnum, partialMembers, dataMemberOffset, inferTagByName, implicitMode, members, member, ref forced, isPublic, isField, ref effectiveType);
  650. } else if ((field = member as FieldInfo) != null)
  651. {
  652. effectiveType = field.FieldType;
  653. isPublic = field.IsPublic;
  654. isField = true;
  655. if (isEnum && !field.IsStatic)
  656. { // only care about static things on enums; WinRT has a __value instance field!
  657. continue;
  658. }
  659. ApplyDefaultBehaviour_AddMembers(model, family, isEnum, partialMembers, dataMemberOffset, inferTagByName, implicitMode, members, member, ref forced, isPublic, isField, ref effectiveType);
  660. } else if ((method = member as MethodInfo) != null)
  661. {
  662. if (isEnum) continue;
  663. AttributeMap[] memberAttribs = AttributeMap.Create(model, method, false);
  664. if (memberAttribs != null && memberAttribs.Length > 0)
  665. {
  666. CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoBeforeSerializationAttribute", ref callbacks, 0);
  667. CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoAfterSerializationAttribute", ref callbacks, 1);
  668. CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoBeforeDeserializationAttribute", ref callbacks, 2);
  669. CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoAfterDeserializationAttribute", ref callbacks, 3);
  670. CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnSerializingAttribute", ref callbacks, 4);
  671. CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnSerializedAttribute", ref callbacks, 5);
  672. CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnDeserializingAttribute", ref callbacks, 6);
  673. CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnDeserializedAttribute", ref callbacks, 7);
  674. }
  675. }
  676. }
  677. ProtoMemberAttribute[] arr = new ProtoMemberAttribute[members.Count];
  678. members.CopyTo(arr, 0);
  679. if (inferTagByName || implicitMode != ImplicitFields.None)
  680. {
  681. Array.Sort(arr);
  682. int nextTag = implicitFirstTag;
  683. foreach (ProtoMemberAttribute normalizedAttribute in arr)
  684. {
  685. if (!normalizedAttribute.TagIsPinned) // if ProtoMember etc sets a tag, we'll trust it
  686. {
  687. normalizedAttribute.Rebase(nextTag++);
  688. }
  689. }
  690. }
  691. foreach (ProtoMemberAttribute normalizedAttribute in arr)
  692. {
  693. ValueMember vm = ApplyDefaultBehaviour(isEnum, normalizedAttribute);
  694. if (vm != null)
  695. {
  696. Add(vm);
  697. }
  698. }
  699. if (callbacks != null)
  700. {
  701. SetCallbacks(Coalesce(callbacks, 0, 4), Coalesce(callbacks, 1, 5),
  702. Coalesce(callbacks, 2, 6), Coalesce(callbacks, 3, 7));
  703. }
  704. }
  705. private static void ApplyDefaultBehaviour_AddMembers(TypeModel model, AttributeFamily family, bool isEnum, BasicList partialMembers, int dataMemberOffset, bool inferTagByName, ImplicitFields implicitMode, BasicList members, MemberInfo member, ref bool forced, bool isPublic, bool isField, ref Type effectiveType)
  706. {
  707. switch (implicitMode)
  708. {
  709. case ImplicitFields.AllFields:
  710. if (isField) forced = true;
  711. break;
  712. case ImplicitFields.AllPublic:
  713. if (isPublic) forced = true;
  714. break;
  715. }
  716. // we just don't like delegate types ;p
  717. #if WINRT || COREFX
  718. if (effectiveType.GetTypeInfo().IsSubclassOf(typeof(Delegate))) effectiveType = null;
  719. #else
  720. if (effectiveType.IsSubclassOf(model.MapType(typeof(Delegate)))) effectiveType = null;
  721. #endif
  722. if (effectiveType != null)
  723. {
  724. ProtoMemberAttribute normalizedAttribute = NormalizeProtoMember(model, member, family, forced, isEnum, partialMembers, dataMemberOffset, inferTagByName);
  725. if (normalizedAttribute != null) members.Add(normalizedAttribute);
  726. }
  727. }
  728. static MethodInfo Coalesce(MethodInfo[] arr, int x, int y)
  729. {
  730. MethodInfo mi = arr[x];
  731. if (mi == null) mi = arr[y];
  732. return mi;
  733. }
  734. internal static AttributeFamily GetContractFamily(RuntimeTypeModel model, Type type, AttributeMap[] attributes)
  735. {
  736. AttributeFamily family = AttributeFamily.None;
  737. if (attributes == null) attributes = AttributeMap.Create(model, type, false);
  738. for (int i = 0; i < attributes.Length; i++)
  739. {
  740. switch (attributes[i].AttributeType.FullName)
  741. {
  742. case "ProtoBuf.ProtoContractAttribute":
  743. bool tmp = false;
  744. GetFieldBoolean(ref tmp, attributes[i], "UseProtoMembersOnly");
  745. if (tmp) return AttributeFamily.ProtoBuf;
  746. family |= AttributeFamily.ProtoBuf;
  747. break;
  748. case "System.Xml.Serialization.XmlTypeAttribute":
  749. if (!model.AutoAddProtoContractTypesOnly)
  750. {
  751. family |= AttributeFamily.XmlSerializer;
  752. }
  753. break;
  754. case "System.Runtime.Serialization.DataContractAttribute":
  755. if (!model.AutoAddProtoContractTypesOnly)
  756. {
  757. family |= AttributeFamily.DataContractSerialier;
  758. }
  759. break;
  760. }
  761. }
  762. if(family == AttributeFamily.None)
  763. { // check for obvious tuples
  764. MemberInfo[] mapping;
  765. if(ResolveTupleConstructor(type, out mapping) != null)
  766. {
  767. family |= AttributeFamily.AutoTuple;
  768. }
  769. }
  770. return family;
  771. }
  772. internal static ConstructorInfo ResolveTupleConstructor(Type type, out MemberInfo[] mappedMembers)
  773. {
  774. mappedMembers = null;
  775. if(type == null) throw new ArgumentNullException("type");
  776. #if WINRT || COREFX
  777. TypeInfo typeInfo = type.GetTypeInfo();
  778. if (typeInfo.IsAbstract) return null; // as if!
  779. ConstructorInfo[] ctors = Helpers.GetConstructors(typeInfo, false);
  780. #else
  781. if(type.IsAbstract) return null; // as if!
  782. ConstructorInfo[] ctors = Helpers.GetConstructors(type, false);
  783. #endif
  784. // need to have an interesting constructor to bother even checking this stuff
  785. if(ctors.Length == 0 || (ctors.Length == 1 && ctors[0].GetParameters().Length == 0)) return null;
  786. MemberInfo[] fieldsPropsUnfiltered = Helpers.GetInstanceFieldsAndProperties(type, true);
  787. BasicList memberList = new BasicList();
  788. for (int i = 0; i < fieldsPropsUnfiltered.Length; i++)
  789. {
  790. PropertyInfo prop = fieldsPropsUnfiltered[i] as PropertyInfo;
  791. if (prop != null)
  792. {
  793. if (!prop.CanRead) return null; // no use if can't read
  794. if (prop.CanWrite && Helpers.GetSetMethod(prop, false, false) != null) return null; // don't allow a public set (need to allow non-public to handle Mono's KeyValuePair<,>)
  795. memberList.Add(prop);
  796. }
  797. else
  798. {
  799. FieldInfo field = fieldsPropsUnfiltered[i] as FieldInfo;
  800. if (field != null)
  801. {
  802. if (!field.IsInitOnly) return null; // all public fields must be readonly to be counted a tuple
  803. memberList.Add(field);
  804. }
  805. }
  806. }
  807. if (memberList.Count == 0)
  808. {
  809. return null;
  810. }
  811. MemberInfo[] members = new MemberInfo[memberList.Count];
  812. memberList.CopyTo(members, 0);
  813. int[] mapping = new int[members.Length];
  814. int found = 0;
  815. ConstructorInfo result = null;
  816. mappedMembers = new MemberInfo[mapping.Length];
  817. for(int i = 0 ; i < ctors.Length ; i++)
  818. {
  819. ParameterInfo[] parameters = ctors[i].GetParameters();
  820. if (parameters.Length != members.Length) continue;
  821. // reset the mappings to test
  822. for (int j = 0; j < mapping.Length; j++) mapping[j] = -1;
  823. for(int j = 0 ; j < parameters.Length ; j++)
  824. {
  825. for(int k = 0 ; k < members.Length ; k++)
  826. {
  827. if (string.Compare(parameters[j].Name, members[k].Name, StringComparison.OrdinalIgnoreCase) != 0) continue;
  828. Type memberType = Helpers.GetMemberType(members[k]);
  829. if (memberType != parameters[j].ParameterType) continue;
  830. mapping[j] = k;
  831. }
  832. }
  833. // did we map all?
  834. bool notMapped = false;
  835. for (int j = 0; j < mapping.Length; j++)
  836. {
  837. if (mapping[j] < 0)
  838. {
  839. notMapped = true;
  840. break;
  841. }
  842. mappedMembers[j] = members[mapping[j]];
  843. }
  844. if (notMapped) continue;
  845. found++;
  846. result = ctors[i];
  847. }
  848. return found == 1 ? result : null;
  849. }
  850. private static void CheckForCallback(MethodInfo method, AttributeMap[] attributes, string callbackTypeName, ref MethodInfo[] callbacks, int index)
  851. {
  852. for(int i = 0 ; i < attributes.Length ; i++)
  853. {
  854. if(attributes[i].AttributeType.FullName == callbackTypeName)
  855. {
  856. if (callbacks == null) { callbacks = new MethodInfo[8]; }
  857. else if (callbacks[index] != null)
  858. {
  859. #if WINRT || FEAT_IKVM || COREFX
  860. Type reflected = method.DeclaringType;
  861. #else
  862. Type reflected = method.ReflectedType;
  863. #endif
  864. throw new ProtoException("Duplicate " + callbackTypeName + " callbacks on " + reflected.FullName);
  865. }
  866. callbacks[index] = method;
  867. }
  868. }
  869. }
  870. private static bool HasFamily(AttributeFamily value, AttributeFamily required)
  871. {
  872. return (value & required) == required;
  873. }
  874. private static ProtoMemberAttribute NormalizeProtoMember(TypeModel model, MemberInfo member, AttributeFamily family, bool forced, bool isEnum, BasicList partialMembers, int dataMemberOffset, bool inferByTagName)
  875. {
  876. if (member == null || (family == AttributeFamily.None && !isEnum)) return null; // nix
  877. int fieldNumber = int.MinValue, minAcceptFieldNumber = inferByTagName ? -1 : 1;
  878. string name = null;
  879. bool isPacked = false, ignore = false, done = false, isRequired = false, asReference = false, asReferenceHasValue = false, dynamicType = false, tagIsPinned = false, overwriteList = false;
  880. DataFormat dataFormat = DataFormat.Default;
  881. if (isEnum) forced = true;
  882. AttributeMap[] attribs = AttributeMap.Create(model, member, true);
  883. AttributeMap attrib;
  884. if (isEnum)
  885. {
  886. attrib = GetAttribute(attribs, "ProtoBuf.ProtoIgnoreAttribute");
  887. if (attrib != null)
  888. {
  889. ignore = true;
  890. }
  891. else
  892. {
  893. attrib = GetAttribute(attribs, "ProtoBuf.ProtoEnumAttribute");
  894. #if WINRT || PORTABLE || CF || FX11 || COREFX
  895. fieldNumber = Convert.ToInt32(((FieldInfo)member).GetValue(null));
  896. #else
  897. fieldNumber = Convert.ToInt32(((FieldInfo)member).GetRawConstantValue());
  898. #endif
  899. if (attrib != null)
  900. {
  901. GetFieldName(ref name, attrib, "Name");
  902. #if !FEAT_IKVM // IKVM can't access HasValue, but conveniently, Value will only be returned if set via ctor or property
  903. if ((bool)Helpers.GetInstanceMethod(attrib.AttributeType
  904. #if WINRT || COREFX
  905. .GetTypeInfo()
  906. #endif
  907. ,"HasValue").Invoke(attrib.Target, null))
  908. #endif
  909. {
  910. object tmp;
  911. if(attrib.TryGet("Value", out tmp)) fieldNumber = (int)tmp;
  912. }
  913. }
  914. }
  915. done = true;
  916. }
  917. if (!ignore && !done) // always consider ProtoMember
  918. {
  919. attrib = GetAttribute(attribs, "ProtoBuf.ProtoMemberAttribute");
  920. GetIgnore(ref ignore, attrib, attribs, "ProtoBuf.ProtoIgnoreAttribute");
  921. if (!ignore && attrib != null)
  922. {
  923. GetFieldNumber(ref fieldNumber, attrib, "Tag");
  924. GetFieldName(ref name, attrib, "Name");
  925. GetFieldBoolean(ref isRequired, attrib, "IsRequired");
  926. GetFieldBoolean(ref isPacked, attrib, "IsPacked");
  927. GetFieldBoolean(ref overwriteList, attrib, "OverwriteList");
  928. GetDataFormat(ref dataFormat, attrib, "DataFormat");
  929. #if !FEAT_IKVM
  930. // IKVM can't access AsReferenceHasValue, but conveniently, AsReference will only be returned if set via ctor or property
  931. GetFieldBoolean(ref asReferenceHasValue, attrib, "AsReferenceHasValue", false);
  932. if(asReferenceHasValue)
  933. #endif
  934. {
  935. asReferenceHasValue = GetFieldBoolean(ref asReference, attrib, "AsReference", true);
  936. }
  937. GetFieldBoolean(ref dynamicType, attrib, "DynamicType");
  938. done = tagIsPinned = fieldNumber > 0; // note minAcceptFieldNumber only applies to non-proto
  939. }
  940. if (!done && partialMembers != null)
  941. {
  942. foreach (AttributeMap ppma in partialMembers)
  943. {
  944. object tmp;
  945. if(ppma.TryGet("MemberName", out tmp) && (string)tmp == member.Name)
  946. {
  947. GetFieldNumber(ref fieldNumber, ppma, "Tag");
  948. GetFieldName(ref name, ppma, "Name");
  949. GetFieldBoolean(ref isRequired, ppma, "IsRequired");
  950. GetFieldBoolean(ref isPacked, ppma, "IsPacked");
  951. GetFieldBoolean(ref overwriteList, attrib, "OverwriteList");
  952. GetDataFormat(ref dataFormat, ppma, "DataFormat");
  953. #if !FEAT_IKVM
  954. // IKVM can't access AsReferenceHasValue, but conveniently, AsReference will only be returned if set via ctor or property
  955. GetFieldBoolean(ref asReferenceHasValue, attrib, "AsReferenceHasValue", false);
  956. if (asReferenceHasValue)
  957. #endif
  958. {
  959. asReferenceHasValue = GetFieldBoolean(ref asReference, ppma, "AsReference", true);
  960. }
  961. GetFieldBoolean(ref dynamicType, ppma, "DynamicType");
  962. if (done = tagIsPinned = fieldNumber > 0) break; // note minAcceptFieldNumber only applies to non-proto
  963. }
  964. }
  965. }
  966. }
  967. if (!ignore && !done && HasFamily(family, AttributeFamily.DataContractSerialier))
  968. {
  969. attrib = GetAttribute(attribs, "System.Runtime.Serialization.DataMemberAttribute");
  970. if (attrib != null)
  971. {
  972. GetFieldNumber(ref fieldNumber, attrib, "Order");
  973. GetFieldName(ref name, attrib, "Name");
  974. GetFieldBoolean(ref isRequired, attrib, "IsRequired");
  975. done = fieldNumber >= minAcceptFieldNumber;
  976. if (done) fieldNumber += dataMemberOffset; // dataMemberOffset only applies to DCS flags, to allow us to "bump" WCF by a notch
  977. }
  978. }
  979. if (!ignore && !done && HasFamily(family, AttributeFamily.XmlSerializer))
  980. {
  981. attrib = GetAttribute(attribs, "System.Xml.Serialization.XmlElementAttribute");
  982. if(attrib == null) attrib = GetAttribute(attribs, "System.Xml.Serialization.XmlArrayAttribute");
  983. GetIgnore(ref ignore, attrib, attribs, "System.Xml.Serialization.XmlIgnoreAttribute");
  984. if (attrib != null && !ignore)
  985. {
  986. GetFieldNumber(ref fieldNumber, attrib, "Order");
  987. GetFieldName(ref name, attrib, "ElementName");
  988. done = fieldNumber >= minAcceptFieldNumber;
  989. }
  990. }
  991. if (!ignore && !done)
  992. {
  993. if (GetAttribute(attribs, "System.NonSerializedAttribute") != null) ignore = true;
  994. }
  995. if (ignore || (fieldNumber < minAcceptFieldNumber && !forced)) return null;
  996. ProtoMemberAttribute result = new ProtoMemberAttribute(fieldNumber, forced || inferByTagName);
  997. result.AsReference = asReference;
  998. result.AsReferenceHasValue = asReferenceHasValue;
  999. result.DataFormat = dataFormat;
  1000. result.DynamicType = dynamicType;
  1001. result.IsPacked = isPacked;
  1002. result.OverwriteList = overwriteList;
  1003. result.IsRequired = isRequired;
  1004. result.Name = Helpers.IsNullOrEmpty(name) ? member.Name : name;
  1005. result.Member = member;
  1006. result.TagIsPinned = tagIsPinned;
  1007. return result;
  1008. }
  1009. private ValueMember ApplyDefaultBehaviour(bool isEnum, ProtoMemberAttribute normalizedAttribute)
  1010. {
  1011. MemberInfo member;
  1012. if (normalizedAttribute == null || (member = normalizedAttribute.Member) == null) return null; // nix
  1013. Type effectiveType = Helpers.GetMemberType(member);
  1014. Type itemType = null;
  1015. Type defaultType = null;
  1016. // check for list types
  1017. ResolveListTypes(model, effectiveType, ref itemType, ref defaultType);
  1018. // but take it back if it is explicitly excluded
  1019. if(itemType != null)
  1020. { // looks like a list, but double check for IgnoreListHandling
  1021. int idx = model.FindOrAddAuto(effectiveType, false, true, false);
  1022. if(idx >= 0 && model[effectiveType].IgnoreListHandling)
  1023. {
  1024. itemType = null;
  1025. defaultType = null;
  1026. }
  1027. }
  1028. AttributeMap[] attribs = AttributeMap.Create(model, member, true);
  1029. AttributeMap attrib;
  1030. object defaultValue = null;
  1031. // implicit zero default
  1032. if (model.UseImplicitZeroDefaults)
  1033. {
  1034. switch (Helpers.GetTypeCode(effectiveType))
  1035. {
  1036. case ProtoTypeCode.Boolean: defaultValue = false; break;
  1037. case ProtoTypeCode.Decimal: defaultValue = (decimal)0; break;
  1038. case ProtoTypeCode.Single: defaultValue = (float)0; break;
  1039. case ProtoTypeCode.Double: defaultValue = (double)0; break;
  1040. case ProtoTypeCode.Byte: defaultValue = (byte)0; break;
  1041. case ProtoTypeCode.Char: defaultValue = (char)0; break;
  1042. case ProtoTypeCode.Int16: defaultValue = (short)0; break;
  1043. case ProtoTypeCode.Int32: defaultValue = (int)0; break;
  1044. case ProtoTypeCode.Int64: defaultValue = (long)0; break;
  1045. case ProtoTypeCode.SByte: defaultValue = (sbyte)0; break;
  1046. case ProtoTypeCode.UInt16: defaultValue = (ushort)0; break;
  1047. case ProtoTypeCode.UInt32: defaultValue = (uint)0; break;
  1048. case ProtoTypeCode.UInt64: defaultValue = (ulong)0; break;
  1049. case ProtoTypeCode.TimeSpan: defaultValue = TimeSpan.Zero; break;
  1050. case ProtoTypeCode.Guid: defaultValue = Guid.Empty; break;
  1051. }
  1052. }
  1053. if ((attrib = GetAttribute(attribs, "System.ComponentModel.DefaultValueAttribute")) != null)
  1054. {
  1055. object tmp;
  1056. if(attrib.TryGet("Value", out tmp)) defaultValue = tmp;
  1057. }
  1058. ValueMember vm = ((isEnum || normalizedAttribute.Tag > 0))
  1059. ? new ValueMember(model, type, normalizedAttribute.Tag, member, effectiveType, itemType, defaultType, normalizedAttribute.DataFormat, defaultValue)
  1060. : null;
  1061. if (vm != null)
  1062. {
  1063. #if WINRT || COREFX
  1064. TypeInfo finalType = typeInfo;
  1065. #else
  1066. Type finalType = type;
  1067. #endif
  1068. PropertyInfo prop = Helpers.GetProperty(finalType, member.Name + "Specified", true);
  1069. MethodInfo getMethod = Helpers.GetGetMethod(prop, true, true);
  1070. if (getMethod == null || getMethod.IsStatic) prop = null;
  1071. if (prop != null)
  1072. {
  1073. vm.SetSpecified(getMethod, Helpers.GetSetMethod(prop, true, true));
  1074. }
  1075. else
  1076. {
  1077. MethodInfo method = Helpers.GetInstanceMethod(finalType, "ShouldSerialize" + member.Name, Helpers.EmptyTypes);
  1078. if (method != null && method.ReturnType == model.MapType(typeof(bool)))
  1079. {
  1080. vm.SetSpecified(method, null);
  1081. }
  1082. }
  1083. if (!Helpers.IsNullOrEmpty(normalizedAttribute.Name)) vm.SetName(normalizedAttribute.Name);
  1084. vm.IsPacked = normalizedAttribute.IsPacked;
  1085. vm.IsRequired = normalizedAttribute.IsRequired;
  1086. vm.OverwriteList = normalizedAttribute.OverwriteList;
  1087. if (normalizedAttribute.AsReferenceHasValue)
  1088. {
  1089. vm.AsReference = normalizedAttribute.AsReference;
  1090. }
  1091. vm.DynamicType = normalizedAttribute.DynamicType;
  1092. }
  1093. return vm;
  1094. }
  1095. private static void GetDataFormat(ref DataFormat value, AttributeMap attrib, string memberName)
  1096. {
  1097. if ((attrib == null) || (value != DataFormat.Default)) return;
  1098. object obj;
  1099. if (attrib.TryGet(memberName, out obj) && obj != null) value = (DataFormat)obj;
  1100. }
  1101. private static void GetIgnore(ref bool ignore, AttributeMap attrib, AttributeMap[] attribs, string fullName)
  1102. {
  1103. if (ignore || attrib == null) return;
  1104. ignore = GetAttribute(attribs, fullName) != null;
  1105. return;
  1106. }
  1107. private static void GetFieldBoolean(ref bool value, AttributeMap attrib, string memberName)
  1108. {
  1109. GetFieldBoolean(ref value, attrib, memberName, true);
  1110. }
  1111. private static bool GetFieldBoolean(ref bool value, AttributeMap attrib, string memberName, bool publicOnly)
  1112. {
  1113. if (attrib == null) return false;
  1114. if (value) return true;
  1115. object obj;
  1116. if (attrib.TryGet(memberName, publicOnly, out obj) && obj != null)
  1117. {
  1118. value = (bool)obj;
  1119. return true;
  1120. }
  1121. return false;
  1122. }
  1123. private static void GetFieldNumber(ref int value, AttributeMap attrib, string memberName)
  1124. {
  1125. if (attrib == null || value > 0) return;
  1126. object obj;
  1127. if (attrib.TryGet(memberName, out obj) && obj != null) value = (int)obj;
  1128. }
  1129. private static void GetFieldName(ref string name, AttributeMap attrib, string memberName)
  1130. {
  1131. if (attrib == null || !Helpers.IsNullOrEmpty(name)) return;
  1132. object obj;
  1133. if (attrib.TryGet(memberName, out obj) && obj != null) name = (string)obj;
  1134. }
  1135. private static AttributeMap GetAttribute(AttributeMap[] attribs, string fullName)
  1136. {
  1137. for (int i = 0; i < attribs.Length; i++)
  1138. {
  1139. AttributeMap attrib = attribs[i];
  1140. if (attrib != null && attrib.AttributeType.FullName == fullName) return attrib;
  1141. }
  1142. return null;
  1143. }
  1144. /// <summary>
  1145. /// Adds a member (by name) to the MetaType
  1146. /// </summary>
  1147. public MetaType Add(int fieldNumber, string memberName)
  1148. {
  1149. AddField(fieldNumber, memberName, null, null, null);
  1150. return this;
  1151. }
  1152. /// <summary>
  1153. /// Adds a member (by name) to the MetaType, returning the ValueMember rather than the fluent API.
  1154. /// This is otherwise identical to Add.
  1155. /// </summary>
  1156. public ValueMember AddField(int fieldNumber, string memberName)
  1157. {
  1158. return AddField(fieldNumber, memberName, null, null, null);
  1159. }
  1160. /// <summary>
  1161. /// Gets or sets whether the type should use a parameterless constructor (the default),
  1162. /// or whether the type should skip the constructor completely. This option is not supported
  1163. /// on compact-framework.
  1164. /// </summary>
  1165. public bool UseConstructor
  1166. { // negated to have defaults as flat zero
  1167. get { return !HasFlag(OPTIONS_SkipConstructor); }
  1168. set { SetFlag(OPTIONS_SkipConstructor, !value, true); }
  1169. }
  1170. /// <summary>
  1171. /// The concrete type to create when a new instance of this type is needed; this may be useful when dealing
  1172. /// with dynamic proxies, or with interface-based APIs
  1173. /// </summary>
  1174. public Type ConstructType
  1175. {
  1176. get { return constructType; }
  1177. set
  1178. {
  1179. ThrowIfFrozen();
  1180. constructType = value;
  1181. }
  1182. }
  1183. private Type constructType;
  1184. /// <summary>
  1185. /// Adds a member (by name) to the MetaType
  1186. /// </summary>
  1187. public MetaType Add(string memberName)
  1188. {
  1189. Add(GetNextFieldNumber(), memberName);
  1190. return this;
  1191. }
  1192. Type surrogate;
  1193. /// <summary>
  1194. /// Performs serialization of this type via a surrogate; all
  1195. /// other serialization options are ignored and handled
  1196. /// by the surrogate's configuration.
  1197. /// </summary>
  1198. public void SetSurrogate(Type surrogateType)
  1199. {
  1200. if (surrogateType == type) surrogateType = null;
  1201. if (surrogateType != null)
  1202. {
  1203. // note that BuildSerializer checks the **CURRENT TYPE** is OK to be surrogated
  1204. if (surrogateType != null && Helpers.IsAssignableFrom(model.MapType(typeof(IEnumerable)), surrogateType))
  1205. {
  1206. throw new ArgumentException("Repeated data (a list, collection, etc) has inbuilt behaviour and cannot be used as a surrogate");
  1207. }
  1208. }
  1209. ThrowIfFrozen();
  1210. this.surrogate = surrogateType;
  1211. // no point in offering chaining; no options are respected
  1212. }
  1213. internal MetaType GetSurrogateOrSelf()
  1214. {
  1215. if (surrogate != null) return model[surrogate];
  1216. return this;
  1217. }
  1218. internal MetaType GetSurrogateOrBaseOrSelf(bool deep) {
  1219. if(surrogate != null) return model[surrogate];
  1220. MetaType snapshot = this.baseType;
  1221. if (snapshot != null)
  1222. {
  1223. if (deep)
  1224. {
  1225. MetaType tmp;
  1226. do
  1227. {
  1228. tmp = snapshot;
  1229. snapshot = snapshot.baseType;
  1230. } while(snapshot != null);
  1231. return tmp;
  1232. }
  1233. return snapshot;
  1234. }
  1235. return this;
  1236. }
  1237. private int GetNextFieldNumber()
  1238. {
  1239. int maxField = 0;
  1240. foreach (ValueMember member in fields)
  1241. {
  1242. if (member.FieldNumber > maxField) maxField = member.FieldNumber;
  1243. }
  1244. if (subTypes != null)
  1245. {
  1246. foreach (SubType subType in subTypes)
  1247. {
  1248. if (subType.FieldNumber > maxField) maxField = subType.FieldNumber;
  1249. }
  1250. }
  1251. return maxField + 1;
  1252. }
  1253. /// <summary>
  1254. /// Adds a set of members (by name) to the MetaType
  1255. /// </summary>
  1256. public MetaType Add(params string[] memberNames)
  1257. {
  1258. if (memberNames == null) throw new ArgumentNullException("memberNames");
  1259. int next = GetNextFieldNumber();
  1260. for (int i = 0; i < memberNames.Length; i++)
  1261. {
  1262. Add(next++, memberNames[i]);
  1263. }
  1264. return this;
  1265. }
  1266. /// <summary>
  1267. /// Adds a member (by name) to the MetaType
  1268. /// </summary>
  1269. public MetaType Add(int fieldNumber, string memberName, object defaultValue)
  1270. {
  1271. AddField(fieldNumber, memberName, null, null, defaultValue);
  1272. return this;
  1273. }
  1274. /// <summary>
  1275. /// Adds a member (by name) to the MetaType, including an itemType and defaultType for representing lists
  1276. /// </summary>
  1277. public MetaType Add(int fieldNumber, string memberName, Type itemType, Type defaultType)
  1278. {
  1279. AddField(fieldNumber, memberName, itemType, defaultType, null);
  1280. return this;
  1281. }
  1282. /// <summary>
  1283. /// Adds a member (by name) to the MetaType, including an itemType and defaultType for representing lists, returning the ValueMember rather than the fluent API.
  1284. /// This is otherwise identical to Add.
  1285. /// </summary>
  1286. public ValueMember AddField(int fieldNumber, string memberName, Type itemType, Type defaultType)
  1287. {
  1288. return AddField(fieldNumber, memberName, itemType, defaultType, null);
  1289. }
  1290. private ValueMember AddField(int fieldNumber, string memberName, Type itemType, Type defaultType, object defaultValue)
  1291. {
  1292. MemberInfo mi = null;
  1293. #if WINRT
  1294. mi = Helpers.IsEnum(type) ? type.GetTypeInfo().GetDeclaredField(memberName) : Helpers.GetInstanceMember(type.GetTypeInfo(), memberName);
  1295. #else
  1296. MemberInfo[] members = type.GetMember(memberName, Helpers.IsEnum(type) ? BindingFlags.Static | BindingFlags.Public : BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  1297. if(members != null && members.Length == 1) mi = members[0];
  1298. #endif
  1299. if (mi == null) throw new ArgumentException("Unable to determine member: " + memberName, "memberName");
  1300. Type miType;
  1301. #if WINRT || PORTABLE || COREFX
  1302. PropertyInfo pi = mi as PropertyInfo;
  1303. if (pi == null)
  1304. {
  1305. FieldInfo fi = mi as FieldInfo;
  1306. if (fi == null)
  1307. {
  1308. throw new NotSupportedException(mi.GetType().Name);
  1309. }
  1310. else
  1311. {
  1312. miType = fi.FieldType;
  1313. }
  1314. }
  1315. else
  1316. {
  1317. miType = pi.PropertyType;
  1318. }
  1319. #else
  1320. switch (mi.MemberType)
  1321. {
  1322. case MemberTypes.Field:
  1323. miType = ((FieldInfo)mi).FieldType; break;
  1324. case MemberTypes.Property:
  1325. miType = ((PropertyInfo)mi).PropertyType; break;
  1326. default:
  1327. throw new NotSupportedException(mi.MemberType.ToString());
  1328. }
  1329. #endif
  1330. ResolveListTypes(model, miType, ref itemType, ref defaultType);
  1331. ValueMember newField = new ValueMember(model, type, fieldNumber, mi, miType, itemType, defaultType, DataFormat.Default, defaultValue);
  1332. Add(newField);
  1333. return newField;
  1334. }
  1335. internal static void ResolveListTypes(TypeModel model, Type type, ref Type itemType, ref Type defaultType)
  1336. {
  1337. if (type == null) return;
  1338. // handle arrays
  1339. if (type.IsArray)
  1340. {
  1341. if (type.GetArrayRank() != 1)
  1342. {
  1343. throw new NotSupportedException("Multi-dimensional arrays are not supported");
  1344. }
  1345. itemType = type.GetElementType();
  1346. if (itemType == model.MapType(typeof(byte)))
  1347. {
  1348. defaultType = itemType = null;
  1349. }
  1350. else
  1351. {
  1352. defaultType = type;
  1353. }
  1354. }
  1355. // handle lists
  1356. if (itemType == null) { itemType = TypeModel.GetListItemType(model, type); }
  1357. // check for nested data (not allowed)
  1358. if (itemType != null)
  1359. {
  1360. Type nestedItemType = null, nestedDefaultType = null;
  1361. ResolveListTypes(model, itemType, ref nestedItemType, ref nestedDefaultType);
  1362. if (nestedItemType != null)
  1363. {
  1364. throw TypeModel.CreateNestedListsNotSupported();
  1365. }
  1366. }
  1367. if (itemType != null && defaultType == null)
  1368. {
  1369. #if WINRT || COREFX
  1370. TypeInfo typeInfo = type.GetTypeInfo();
  1371. if (typeInfo.IsClass && !typeInfo.IsAbstract && Helpers.GetConstructor(typeInfo, Helpers.EmptyTypes, true) != null)
  1372. #else
  1373. if (type.IsClass && !type.IsAbstract && Helpers.GetConstructor(type, Helpers.EmptyTypes, true) != null)
  1374. #endif
  1375. {
  1376. defaultType = type;
  1377. }
  1378. if (defaultType == null)
  1379. {
  1380. #if WINRT || COREFX
  1381. if (typeInfo.IsInterface)
  1382. #else
  1383. if (type.IsInterface)
  1384. #endif
  1385. {
  1386. #if NO_GENERICS
  1387. defaultType = typeof(ArrayList);
  1388. #else
  1389. Type[] genArgs;
  1390. #if WINRT || COREFX
  1391. if (typeInfo.IsGenericType && type.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IDictionary<,>)
  1392. && itemType == typeof(System.Collections.Generic.KeyValuePair<,>).MakeGenericType(genArgs = typeInfo.GenericTypeArguments))
  1393. #else
  1394. if (type.IsGenericType && type.GetGenericTypeDefinition() == model.MapType(typeof(System.Collections.Generic.IDictionary<,>))
  1395. && itemType == model.MapType(typeof(System.Collections.Generic.KeyValuePair<,>)).MakeGenericType(genArgs = type.GetGenericArguments()))
  1396. #endif
  1397. {
  1398. defaultType = model.MapType(typeof(System.Collections.Generic.Dictionary<,>)).MakeGenericType(genArgs);
  1399. }
  1400. else
  1401. {
  1402. defaultType = model.MapType(typeof(System.Collections.Generic.List<>)).MakeGenericType(itemType);
  1403. }
  1404. #endif
  1405. }
  1406. }
  1407. // verify that the default type is appropriate
  1408. if (defaultType != null && !Helpers.IsAssignableFrom(type, defaultType)) { defaultType = null; }
  1409. }
  1410. }
  1411. private void Add(ValueMember member) {
  1412. int opaqueToken = 0;
  1413. try {
  1414. model.TakeLock(ref opaqueToken);
  1415. ThrowIfFrozen();
  1416. fields.Add(member);
  1417. } finally
  1418. {
  1419. model.ReleaseLock(opaqueToken);
  1420. }
  1421. }
  1422. /// <summary>
  1423. /// Returns the ValueMember that matchs a given field number, or null if not found
  1424. /// </summary>
  1425. public ValueMember this[int fieldNumber]
  1426. {
  1427. get
  1428. {
  1429. foreach (ValueMember member in fields)
  1430. {
  1431. if (member.FieldNumber == fieldNumber) return member;
  1432. }
  1433. return null;
  1434. }
  1435. }
  1436. /// <summary>
  1437. /// Returns the ValueMember that matchs a given member (property/field), or null if not found
  1438. /// </summary>
  1439. public ValueMember this[MemberInfo member]
  1440. {
  1441. get
  1442. {
  1443. if (member == null) return null;
  1444. foreach (ValueMember x in fields)
  1445. {
  1446. if (x.Member == member) return x;
  1447. }
  1448. return null;
  1449. }
  1450. }
  1451. private readonly BasicList fields = new BasicList();
  1452. /// <summary>
  1453. /// Returns the ValueMember instances associated with this type
  1454. /// </summary>
  1455. public ValueMember[] GetFields() {
  1456. ValueMember[] arr = new ValueMember[fields.Count];
  1457. fields.CopyTo(arr, 0);
  1458. Array.Sort(arr, ValueMember.Comparer.Default);
  1459. return arr;
  1460. }
  1461. /// <summary>
  1462. /// Returns the SubType instances associated with this type
  1463. /// </summary>
  1464. public SubType[] GetSubtypes()
  1465. {
  1466. if (subTypes == null || subTypes.Count == 0) return new SubType[0];
  1467. SubType[] arr = new SubType[subTypes.Count];
  1468. subTypes.CopyTo(arr, 0);
  1469. Array.Sort(arr, SubType.Comparer.Default);
  1470. return arr;
  1471. }
  1472. #if FEAT_COMPILER && !FX11
  1473. /// <summary>
  1474. /// Compiles the serializer for this type; this is *not* a full
  1475. /// standalone compile, but can significantly boost performance
  1476. /// while allowing additional types to be added.
  1477. /// </summary>
  1478. /// <remarks>An in-place compile can access non-public types / members</remarks>
  1479. public void CompileInPlace()
  1480. {
  1481. #if FEAT_IKVM
  1482. // just no nothing, quietely; don't want to break the API
  1483. #else
  1484. serializer = CompiledSerializer.Wrap(Serializer, model);
  1485. #endif
  1486. }
  1487. #endif
  1488. internal bool IsDefined(int fieldNumber)
  1489. {
  1490. foreach (ValueMember field in fields)
  1491. {
  1492. if (field.FieldNumber == fieldNumber) return true;
  1493. }
  1494. return false;
  1495. }
  1496. internal int GetKey(bool demand, bool getBaseKey)
  1497. {
  1498. return model.GetKey(type, demand, getBaseKey);
  1499. }
  1500. internal EnumSerializer.EnumPair[] GetEnumMap()
  1501. {
  1502. if (HasFlag(OPTIONS_EnumPassThru)) return null;
  1503. EnumSerializer.EnumPair[] result = new EnumSerializer.EnumPair[fields.Count];
  1504. for (int i = 0; i < result.Length; i++)
  1505. {
  1506. ValueMember member = (ValueMember) fields[i];
  1507. int wireValue = member.FieldNumber;
  1508. object value = member.GetRawEnumValue();
  1509. result[i] = new EnumSerializer.EnumPair(wireValue, value, member.MemberType);
  1510. }
  1511. return result;
  1512. }
  1513. /// <summary>
  1514. /// Gets or sets a value indicating that an enum should be treated directly as an int/short/etc, rather
  1515. /// than enforcing .proto enum rules. This is useful *in particul* for [Flags] enums.
  1516. /// </summary>
  1517. public bool EnumPassthru
  1518. {
  1519. get { return HasFlag(OPTIONS_EnumPassThru); }
  1520. set { SetFlag(OPTIONS_EnumPassThru, value, true); }
  1521. }
  1522. /// <summary>
  1523. /// Gets or sets a value indicating that this type should NOT be treated as a list, even if it has
  1524. /// familiar list-like characteristics (enumerable, add, etc)
  1525. /// </summary>
  1526. public bool IgnoreListHandling
  1527. {
  1528. get { return HasFlag(OPTIONS_IgnoreListHandling); }
  1529. set { SetFlag(OPTIONS_IgnoreListHandling, value, true); }
  1530. }
  1531. internal bool Pending
  1532. {
  1533. get { return HasFlag(OPTIONS_Pending); }
  1534. set { SetFlag(OPTIONS_Pending, value, false); }
  1535. }
  1536. private const byte
  1537. OPTIONS_Pending = 1,
  1538. OPTIONS_EnumPassThru = 2,
  1539. OPTIONS_Frozen = 4,
  1540. OPTIONS_PrivateOnApi = 8,
  1541. OPTIONS_SkipConstructor = 16,
  1542. OPTIONS_AsReferenceDefault = 32,
  1543. OPTIONS_AutoTuple = 64,
  1544. OPTIONS_IgnoreListHandling = 128;
  1545. private volatile byte flags;
  1546. private bool HasFlag(byte flag) { return (flags & flag) == flag; }
  1547. private void SetFlag(byte flag, bool value, bool throwIfFrozen)
  1548. {
  1549. if (throwIfFrozen && HasFlag(flag) != value)
  1550. {
  1551. ThrowIfFrozen();
  1552. }
  1553. if (value)
  1554. flags |= flag;
  1555. else
  1556. flags = (byte)(flags & ~flag);
  1557. }
  1558. internal static MetaType GetRootType(MetaType source)
  1559. {
  1560. while (source.serializer != null)
  1561. {
  1562. MetaType tmp = source.baseType;
  1563. if (tmp == null) return source;
  1564. source = tmp; // else loop until we reach something that isn't generated, or is the root
  1565. }
  1566. // now we get into uncertain territory
  1567. RuntimeTypeModel model = source.model;
  1568. int opaqueToken = 0;
  1569. try {
  1570. model.TakeLock(ref opaqueToken);
  1571. MetaType tmp;
  1572. while ((tmp = source.baseType) != null) source = tmp;
  1573. return source;
  1574. } finally {
  1575. model.ReleaseLock(opaqueToken);
  1576. }
  1577. }
  1578. internal bool IsPrepared()
  1579. {
  1580. #if FEAT_COMPILER && !FEAT_IKVM && !FX11
  1581. return serializer is CompiledSerializer;
  1582. #else
  1583. return false;
  1584. #endif
  1585. }
  1586. internal System.Collections.IEnumerable Fields { get { return this.fields; } }
  1587. internal static System.Text.StringBuilder NewLine(System.Text.StringBuilder builder, int indent)
  1588. {
  1589. return Helpers.AppendLine(builder).Append(' ', indent*3);
  1590. }
  1591. internal bool IsAutoTuple
  1592. {
  1593. get { return HasFlag(OPTIONS_AutoTuple); }
  1594. }
  1595. internal void WriteSchema(System.Text.StringBuilder builder, int indent, ref bool requiresBclImport)
  1596. {
  1597. if (surrogate != null) return; // nothing to write
  1598. ValueMember[] fieldsArr = new ValueMember[fields.Count];
  1599. fields.CopyTo(fieldsArr, 0);
  1600. Array.Sort(fieldsArr, ValueMember.Comparer.Default);
  1601. if (IsList)
  1602. {
  1603. string itemTypeName = model.GetSchemaTypeName(TypeModel.GetListItemType(model, type), DataFormat.Default, false, false, ref requiresBclImport);
  1604. NewLine(builder, indent).Append("message ").Append(GetSchemaTypeName()).Append(" {");
  1605. NewLine(builder, indent + 1).Append("repeated ").Append(itemTypeName).Append(" items = 1;");
  1606. NewLine(builder, indent).Append('}');
  1607. }
  1608. else if (IsAutoTuple)
  1609. { // key-value-pair etc
  1610. MemberInfo[] mapping;
  1611. if(ResolveTupleConstructor(type, out mapping) != null)
  1612. {
  1613. NewLine(builder, indent).Append("message ").Append(GetSchemaTypeName()).Append(" {");
  1614. for(int i = 0 ; i < mapping.Length ; i++)
  1615. {
  1616. Type effectiveType;
  1617. if(mapping[i] is PropertyInfo)
  1618. {
  1619. effectiveType = ((PropertyInfo) mapping[i]).PropertyType;
  1620. } else if (mapping[i] is FieldInfo)
  1621. {
  1622. effectiveType = ((FieldInfo) mapping[i]).FieldType;
  1623. } else
  1624. {
  1625. throw new NotSupportedException("Unknown member type: " + mapping[i].GetType().Name);
  1626. }
  1627. NewLine(builder, indent + 1).Append("optional ").Append(model.GetSchemaTypeName(effectiveType, DataFormat.Default, false, false, ref requiresBclImport).Replace('.','_'))
  1628. .Append(' ').Append(mapping[i].Name).Append(" = ").Append(i + 1).Append(';');
  1629. }
  1630. NewLine(builder, indent).Append('}');
  1631. }
  1632. }
  1633. else if(Helpers.IsEnum(type))
  1634. {
  1635. NewLine(builder, indent).Append("enum ").Append(GetSchemaTypeName()).Append(" {");
  1636. if (fieldsArr.Length == 0 && EnumPassthru) {
  1637. if (type
  1638. #if WINRT || COREFX
  1639. .GetTypeInfo()
  1640. #endif
  1641. .IsDefined(model.MapType(typeof(FlagsAttribute)), false))
  1642. {
  1643. NewLine(builder, indent + 1).Append("// this is a composite/flags enumeration");
  1644. }
  1645. else
  1646. {
  1647. NewLine(builder, indent + 1).Append("// this enumeration will be passed as a raw value");
  1648. }
  1649. foreach(FieldInfo field in
  1650. #if WINRT
  1651. type.GetRuntimeFields()
  1652. #else
  1653. type.GetFields()
  1654. #endif
  1655. )
  1656. {
  1657. if(field.IsStatic && field.IsLiteral)
  1658. {
  1659. object enumVal;
  1660. #if WINRT || PORTABLE || CF || FX11 || NETSTANDARD1_3 || NETSTANDARD1_4
  1661. enumVal = Convert.ChangeType(field.GetValue(null), Enum.GetUnderlyingType(field.FieldType));
  1662. #else
  1663. enumVal = field.GetRawConstantValue();
  1664. #endif
  1665. NewLine(builder, indent + 1).Append(field.Name).Append(" = ").Append(enumVal).Append(";");
  1666. }
  1667. }
  1668. }
  1669. else
  1670. {
  1671. foreach (ValueMember member in fieldsArr)
  1672. {
  1673. NewLine(builder, indent + 1).Append(member.Name).Append(" = ").Append(member.FieldNumber).Append(';');
  1674. }
  1675. }
  1676. NewLine(builder, indent).Append('}');
  1677. } else
  1678. {
  1679. NewLine(builder, indent).Append("message ").Append(GetSchemaTypeName()).Append(" {");
  1680. foreach (ValueMember member in fieldsArr)
  1681. {
  1682. string ordinality = member.ItemType != null ? "repeated" : member.IsRequired ? "required" : "optional";
  1683. NewLine(builder, indent + 1).Append(ordinality).Append(' ');
  1684. if (member.DataFormat == DataFormat.Group) builder.Append("group ");
  1685. string schemaTypeName = member.GetSchemaTypeName(true, ref requiresBclImport);
  1686. builder.Append(schemaTypeName).Append(" ")
  1687. .Append(member.Name).Append(" = ").Append(member.FieldNumber);
  1688. if(member.DefaultValue != null && member.IsRequired == false)
  1689. {
  1690. if (member.DefaultValue is string)
  1691. {
  1692. builder.Append(" [default = \"").Append(member.DefaultValue).Append("\"]");
  1693. }
  1694. else if(member.DefaultValue is bool)
  1695. { // need to be lower case (issue 304)
  1696. builder.Append((bool)member.DefaultValue ? " [default = true]" : " [default = false]");
  1697. }
  1698. else
  1699. {
  1700. builder.Append(" [default = ").Append(member.DefaultValue).Append(']');
  1701. }
  1702. }
  1703. if(member.ItemType != null && member.IsPacked)
  1704. {
  1705. builder.Append(" [packed=true]");
  1706. }
  1707. builder.Append(';');
  1708. if (schemaTypeName == "bcl.NetObjectProxy" && member.AsReference && !member.DynamicType) // we know what it is; tell the user
  1709. {
  1710. builder.Append(" // reference-tracked ").Append(member.GetSchemaTypeName(false, ref requiresBclImport));
  1711. }
  1712. }
  1713. if (subTypes != null && subTypes.Count != 0)
  1714. {
  1715. NewLine(builder, indent + 1).Append("// the following represent sub-types; at most 1 should have a value");
  1716. SubType[] subTypeArr = new SubType[subTypes.Count];
  1717. subTypes.CopyTo(subTypeArr, 0);
  1718. Array.Sort(subTypeArr, SubType.Comparer.Default);
  1719. foreach (SubType subType in subTypeArr)
  1720. {
  1721. string subTypeName = subType.DerivedType.GetSchemaTypeName();
  1722. NewLine(builder, indent + 1).Append("optional ").Append(subTypeName)
  1723. .Append(" ").Append(subTypeName).Append(" = ").Append(subType.FieldNumber).Append(';');
  1724. }
  1725. }
  1726. NewLine(builder, indent).Append('}');
  1727. }
  1728. }
  1729. }
  1730. }
  1731. #endif