PageRenderTime 67ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Xaml/Test/System.Xaml/TestedTypes.cs

https://bitbucket.org/foobar22/mono
C# | 1305 lines | 1028 code | 236 blank | 41 comment | 30 complexity | dfadcc0f515da3dc7b4132ff627134aa MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Unlicense, Apache-2.0, LGPL-2.0
  1. //
  2. // Copyright (C) 2010 Novell Inc. http://novell.com
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Collections;
  25. using System.Collections.Generic;
  26. using System.Collections.ObjectModel;
  27. using System.ComponentModel;
  28. using System.Diagnostics;
  29. using System.Globalization;
  30. using System.IO;
  31. using System.Linq;
  32. using System.Reflection;
  33. using System.Windows.Markup;
  34. using System.Xaml;
  35. using System.Xaml.Schema;
  36. using System.Xml;
  37. using System.Xml.Schema;
  38. using System.Xml.Serialization;
  39. [assembly: XmlnsDefinition ("http://www.domain.com/path", "XamlTest")] // bug #680385
  40. [assembly: XmlnsDefinition ("http://www.domain.com/path", "SecondTest")] // bug #681045, same xmlns key for different clrns.
  41. [assembly: XmlnsDefinition ("http://schemas.example.com/test", "XamarinBug3003")] // bug #3003
  42. [assembly: XmlnsPrefix ("http://schemas.example.com/test", "test")] // bug #3003
  43. namespace MonoTests.System.Xaml
  44. {
  45. public class ArgumentAttributed
  46. {
  47. public ArgumentAttributed (string s1, string s2)
  48. {
  49. Arg1 = s1;
  50. Arg2 = s2;
  51. }
  52. [ConstructorArgument ("s1")]
  53. public string Arg1 { get; set; }
  54. [ConstructorArgument ("s2")]
  55. public string Arg2 { get; set; }
  56. }
  57. public class ComplexPositionalParameterWrapper
  58. {
  59. public ComplexPositionalParameterWrapper ()
  60. {
  61. }
  62. public ComplexPositionalParameterClass Param { get; set; }
  63. }
  64. [TypeConverter (typeof (ComplexPositionalParameterClassConverter))]
  65. public class ComplexPositionalParameterClass : MarkupExtension
  66. {
  67. public ComplexPositionalParameterClass (ComplexPositionalParameterValue value)
  68. {
  69. this.Value = value;
  70. }
  71. [ConstructorArgument ("value")]
  72. public ComplexPositionalParameterValue Value { get; private set; }
  73. public override object ProvideValue (IServiceProvider sp)
  74. {
  75. return Value.Foo;
  76. }
  77. }
  78. public class ComplexPositionalParameterClassConverter : TypeConverter
  79. {
  80. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  81. {
  82. return sourceType == typeof (string);
  83. }
  84. public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object valueToConvert)
  85. {
  86. return new ComplexPositionalParameterClass (new ComplexPositionalParameterValue () {Foo = (string) valueToConvert});
  87. }
  88. public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
  89. {
  90. // conversion to string is not supported.
  91. return destinationType == typeof (ComplexPositionalParameterClass);
  92. }
  93. }
  94. public class ComplexPositionalParameterValue
  95. {
  96. public string Foo { get; set; }
  97. }
  98. //[MarkupExtensionReturnType (typeof (Array))]
  99. //[ContentProperty ("Items")] ... so, these attributes do not affect XamlObjectReader.
  100. public class MyArrayExtension : MarkupExtension
  101. {
  102. public MyArrayExtension ()
  103. {
  104. items = new ArrayList ();
  105. }
  106. public MyArrayExtension (Array array)
  107. {
  108. items = new ArrayList (array);
  109. this.Type = array.GetType ().GetElementType ();
  110. }
  111. public MyArrayExtension (Type type)
  112. : this ()
  113. {
  114. this.Type = type;
  115. }
  116. IList items;
  117. public IList Items {
  118. get { return items; }
  119. private set { items = value; }
  120. }
  121. [ConstructorArgument ("type")]
  122. public Type Type { get; set; }
  123. public override object ProvideValue (IServiceProvider serviceProvider)
  124. {
  125. if (Type == null)
  126. throw new InvalidOperationException ("Type property must be set before calling ProvideValue method");
  127. Array a = Array.CreateInstance (Type, Items.Count);
  128. Items.CopyTo (a, 0);
  129. return a;
  130. }
  131. }
  132. // The trailing "A" gives significant difference in XML output!
  133. public class MyArrayExtensionA : MarkupExtension
  134. {
  135. public MyArrayExtensionA ()
  136. {
  137. items = new ArrayList ();
  138. }
  139. public MyArrayExtensionA (Array array)
  140. {
  141. items = new ArrayList (array);
  142. this.Type = array.GetType ().GetElementType ();
  143. }
  144. public MyArrayExtensionA (Type type)
  145. : this ()
  146. {
  147. this.Type = type;
  148. }
  149. IList items;
  150. public IList Items {
  151. get { return items; }
  152. private set { items = value; }
  153. }
  154. [ConstructorArgument ("type")]
  155. public Type Type { get; set; }
  156. public override object ProvideValue (IServiceProvider serviceProvider)
  157. {
  158. if (Type == null)
  159. throw new InvalidOperationException ("Type property must be set before calling ProvideValue method");
  160. Array a = Array.CreateInstance (Type, Items.Count);
  161. Items.CopyTo (a, 0);
  162. return a;
  163. }
  164. }
  165. class TestClass1
  166. {
  167. }
  168. public class TestClass3
  169. {
  170. public TestClass3 Nested { get; set; }
  171. }
  172. public class TestClass4
  173. {
  174. public string Foo { get; set; }
  175. public string Bar { get; set; }
  176. }
  177. public class TestClass5
  178. {
  179. public static string Foo { get; set; }
  180. public string Bar { get; set; }
  181. public string Baz { internal get; set; }
  182. public string ReadOnly {
  183. get { return Foo; }
  184. }
  185. }
  186. public class MyExtension : MarkupExtension
  187. {
  188. public MyExtension ()
  189. {
  190. }
  191. public MyExtension (Type arg1, string arg2, string arg3)
  192. {
  193. Foo = arg1;
  194. Bar = arg2;
  195. Baz = arg3;
  196. }
  197. [ConstructorArgument ("arg1")]
  198. public Type Foo { get; set; }
  199. [ConstructorArgument ("arg2")]
  200. public string Bar { get; set; }
  201. [ConstructorArgument ("arg3")]
  202. public string Baz { get; set; }
  203. public override object ProvideValue (IServiceProvider provider)
  204. {
  205. return "provided_value";
  206. }
  207. }
  208. [TypeConverter (typeof (StringConverter))] // This attribute is the markable difference between MyExtension and this type.
  209. public class MyExtension2 : MarkupExtension
  210. {
  211. public MyExtension2 ()
  212. {
  213. }
  214. public MyExtension2 (Type arg1, string arg2)
  215. {
  216. Foo = arg1;
  217. Bar = arg2;
  218. }
  219. [ConstructorArgument ("arg1")]
  220. public Type Foo { get; set; }
  221. [ConstructorArgument ("arg2")]
  222. public string Bar { get; set; }
  223. public override object ProvideValue (IServiceProvider provider)
  224. {
  225. return "provided_value";
  226. }
  227. }
  228. [TypeConverter (typeof (StringConverter))] // same as MyExtension2 except that it is *not* MarkupExtension.
  229. public class MyExtension3
  230. {
  231. public MyExtension3 ()
  232. {
  233. }
  234. // cf. According to [MS-XAML-2009] 3.2.1.11, constructors are invalid unless the type is derived from TypeExtension. So, it is likely *ignored*.
  235. public MyExtension3 (Type arg1, string arg2)
  236. {
  237. Foo = arg1;
  238. Bar = arg2;
  239. }
  240. [ConstructorArgument ("arg1")]
  241. public Type Foo { get; set; }
  242. [ConstructorArgument ("arg2")]
  243. public string Bar { get; set; }
  244. }
  245. [TypeConverter (typeof (DateTimeConverter))] // same as MyExtension3 except for the type converter.
  246. public class MyExtension4
  247. {
  248. public MyExtension4 ()
  249. {
  250. }
  251. // cf. According to [MS-XAML-2009] 3.2.1.11, constructors are invalid unless the type is derived from TypeExtension. So, it is likely *ignored*.
  252. public MyExtension4 (Type arg1, string arg2)
  253. {
  254. Foo = arg1;
  255. Bar = arg2;
  256. }
  257. [ConstructorArgument ("arg1")]
  258. public Type Foo { get; set; }
  259. [ConstructorArgument ("arg2")]
  260. public string Bar { get; set; }
  261. }
  262. // no type converter, and there are only simple-type arguments == _PositionalParameters is applicable.
  263. public class MyExtension5 : MarkupExtension
  264. {
  265. public MyExtension5 (string arg1, string arg2)
  266. {
  267. Foo = arg1;
  268. Bar = arg2;
  269. }
  270. [ConstructorArgument ("arg1")]
  271. public string Foo { get; set; }
  272. [ConstructorArgument ("arg2")]
  273. public string Bar { get; set; }
  274. public override object ProvideValue (IServiceProvider sp)
  275. {
  276. return Foo;
  277. }
  278. }
  279. // Almost the same as MyExtension5, BUT there is default constructor which XamlObjectReader prefers.
  280. public class MyExtension6 : MarkupExtension
  281. {
  282. public MyExtension6 ()
  283. {
  284. }
  285. public MyExtension6 (string arg1)
  286. {
  287. Foo = arg1;
  288. }
  289. [ConstructorArgument ("arg1")]
  290. public string Foo { get; set; }
  291. public override object ProvideValue (IServiceProvider sp)
  292. {
  293. return Foo;
  294. }
  295. }
  296. public class PositionalParametersClass1 : MarkupExtension
  297. {
  298. public PositionalParametersClass1 (string foo)
  299. : this (foo, -1)
  300. {
  301. }
  302. public PositionalParametersClass1 (string foo, int bar)
  303. {
  304. Foo = foo;
  305. Bar = bar;
  306. }
  307. [ConstructorArgument ("foo")]
  308. public string Foo { get; set; }
  309. [ConstructorArgument ("bar")]
  310. public int Bar { get; set; }
  311. public override object ProvideValue (IServiceProvider sp)
  312. {
  313. return Foo;
  314. }
  315. }
  316. public class PositionalParametersWrapper
  317. {
  318. public PositionalParametersClass1 Body { get; set; }
  319. public PositionalParametersWrapper ()
  320. {
  321. }
  322. public PositionalParametersWrapper (string foo, int bar)
  323. {
  324. Body = new PositionalParametersClass1 (foo, bar);
  325. }
  326. }
  327. public class ListWrapper
  328. {
  329. public ListWrapper ()
  330. {
  331. Items = new List<int> ();
  332. }
  333. public ListWrapper (List<int> items)
  334. {
  335. Items = items;
  336. }
  337. public List<int> Items { get; private set; }
  338. }
  339. public class ListWrapper2
  340. {
  341. public ListWrapper2 ()
  342. {
  343. Items = new List<int> ();
  344. }
  345. public ListWrapper2 (List<int> items)
  346. {
  347. Items = items;
  348. }
  349. public List<int> Items { get; set; } // it is settable, which makes difference.
  350. }
  351. [ContentProperty ("Content")]
  352. public class ContentIncludedClass
  353. {
  354. public string Content { get; set; }
  355. }
  356. public class StaticClass1
  357. {
  358. static StaticClass1 ()
  359. {
  360. FooBar = "test";
  361. }
  362. public static string FooBar { get; set; }
  363. }
  364. public class StaticExtensionWrapper
  365. {
  366. public StaticExtensionWrapper ()
  367. {
  368. }
  369. public StaticExtension Param { get; set; }
  370. public static string Foo = "foo";
  371. }
  372. public class TypeExtensionWrapper
  373. {
  374. public TypeExtensionWrapper ()
  375. {
  376. }
  377. public TypeExtension Param { get; set; }
  378. }
  379. public class XDataWrapper
  380. {
  381. public XData Markup { get; set; }
  382. }
  383. // FIXME: test it with XamlXmlReader (needs to create xml first)
  384. public class EventContainer
  385. {
  386. public event Action Run;
  387. }
  388. public class NamedItem
  389. {
  390. public NamedItem ()
  391. {
  392. References = new List<NamedItem> ();
  393. }
  394. public NamedItem (string name)
  395. : this ()
  396. {
  397. ItemName = name;
  398. }
  399. public string ItemName { get; set; }
  400. public IList<NamedItem> References { get; private set; }
  401. }
  402. [RuntimeNameProperty ("ItemName")]
  403. public class NamedItem2
  404. {
  405. public NamedItem2 ()
  406. {
  407. References = new List<NamedItem2> ();
  408. }
  409. public NamedItem2 (string name)
  410. : this ()
  411. {
  412. ItemName = name;
  413. }
  414. public string ItemName { get; set; }
  415. public IList<NamedItem2> References { get; private set; }
  416. }
  417. [TypeConverter (typeof (TestValueConverter))]
  418. public class TestValueSerialized
  419. {
  420. public TestValueSerialized ()
  421. {
  422. }
  423. public string Foo { get; set; }
  424. }
  425. public class TestValueConverter : TypeConverter
  426. {
  427. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  428. {
  429. //Console.Error.WriteLine ("### {0}:{1}", sourceType, context);
  430. ValueSerializerContextTest.Context = (IValueSerializerContext) context;
  431. return true;
  432. }
  433. public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object source)
  434. {
  435. //Console.Error.WriteLine ("##### {0}:{1}", source, context);
  436. ValueSerializerContextTest.Provider = (IServiceProvider) context;
  437. var sp = context as IServiceProvider;
  438. // ValueSerializerContextTest.Context = (IValueSerializerContext) context; -> causes InvalidCastException
  439. if ((source as string) == "v")
  440. return new TestValueSerialized ();
  441. throw new Exception ("huh");
  442. }
  443. public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
  444. {
  445. //Console.Error.WriteLine ("$$$ {0}:{1}", destinationType, context);
  446. ValueSerializerContextTest.Context = (IValueSerializerContext) context;
  447. return destinationType != typeof (MarkupExtension);
  448. }
  449. }
  450. [ContentProperty ("Value")]
  451. public class XmlSerializableWrapper
  452. {
  453. public XmlSerializableWrapper () // mandatory
  454. : this (new XmlSerializable ())
  455. {
  456. }
  457. public XmlSerializableWrapper (XmlSerializable val)
  458. {
  459. this.val = val;
  460. }
  461. XmlSerializable val;
  462. public XmlSerializable Value {
  463. get { return val; }
  464. // To make it become XData, it cannot have a setter.
  465. }
  466. }
  467. public class XmlSerializable : IXmlSerializable
  468. {
  469. public XmlSerializable ()
  470. {
  471. }
  472. public XmlSerializable (string raw)
  473. {
  474. this.raw = raw;
  475. }
  476. string raw;
  477. public string GetRaw ()
  478. {
  479. return raw;
  480. }
  481. public void ReadXml (XmlReader reader)
  482. {
  483. reader.MoveToContent ();
  484. raw = reader.ReadOuterXml ();
  485. }
  486. public void WriteXml (XmlWriter writer)
  487. {
  488. if (raw != null) {
  489. var xr = XmlReader.Create (new StringReader (raw));
  490. while (!xr.EOF)
  491. writer.WriteNode (xr, false);
  492. }
  493. }
  494. public XmlSchema GetSchema ()
  495. {
  496. return null;
  497. }
  498. }
  499. public class Attachable
  500. {
  501. public static readonly AttachableMemberIdentifier FooIdentifier = new AttachableMemberIdentifier (typeof (Attachable), "Foo");
  502. public static readonly AttachableMemberIdentifier ProtectedIdentifier = new AttachableMemberIdentifier (typeof (Attachable), "Protected");
  503. public static string GetFoo (object target)
  504. {
  505. string v;
  506. return AttachablePropertyServices.TryGetProperty (target, FooIdentifier, out v) ? v : null;
  507. }
  508. public static void SetFoo (object target, string value)
  509. {
  510. AttachablePropertyServices.SetProperty (target, FooIdentifier, value);
  511. }
  512. public static string GetBar (object target, object signatureMismatch)
  513. {
  514. return null;
  515. }
  516. public static void SetBar (object signatureMismatch)
  517. {
  518. }
  519. public static void GetBaz (object noReturnType)
  520. {
  521. }
  522. public static string SetBaz (object target, object extraReturnType)
  523. {
  524. return null;
  525. }
  526. protected static string GetProtected (object target)
  527. {
  528. string v;
  529. return AttachablePropertyServices.TryGetProperty (target, ProtectedIdentifier, out v) ? v : null;
  530. }
  531. protected static void SetProtected (object target, string value)
  532. {
  533. AttachablePropertyServices.SetProperty (target, ProtectedIdentifier, value);
  534. }
  535. static Dictionary<object,List<EventHandler>> handlers = new Dictionary<object,List<EventHandler>> ();
  536. public static void AddXHandler (object target, EventHandler handler)
  537. {
  538. List<EventHandler> l;
  539. if (!handlers.TryGetValue (target, out l)) {
  540. l = new List<EventHandler> ();
  541. handlers [target] = l;
  542. }
  543. l.Add (handler);
  544. }
  545. public static void RemoveXHandler (object target, EventHandler handler)
  546. {
  547. handlers [target].Remove (handler);
  548. }
  549. }
  550. public class AttachedPropertyStore : IAttachedPropertyStore
  551. {
  552. public AttachedPropertyStore ()
  553. {
  554. }
  555. Dictionary<AttachableMemberIdentifier,object> props = new Dictionary<AttachableMemberIdentifier,object> ();
  556. public int PropertyCount {
  557. get { return props.Count; }
  558. }
  559. public void CopyPropertiesTo (KeyValuePair<AttachableMemberIdentifier, object> [] array, int index)
  560. {
  561. ((ICollection<KeyValuePair<AttachableMemberIdentifier, object>>) props).CopyTo (array, index);
  562. }
  563. public bool RemoveProperty (AttachableMemberIdentifier attachableMemberIdentifier)
  564. {
  565. return props.Remove (attachableMemberIdentifier);
  566. }
  567. public void SetProperty (AttachableMemberIdentifier attachableMemberIdentifier, object value)
  568. {
  569. props [attachableMemberIdentifier] = value;
  570. }
  571. public bool TryGetProperty (AttachableMemberIdentifier attachableMemberIdentifier, out object value)
  572. {
  573. return props.TryGetValue (attachableMemberIdentifier, out value);
  574. }
  575. }
  576. public class AttachedWrapper : AttachedPropertyStore
  577. {
  578. public AttachedWrapper ()
  579. {
  580. Value = new Attached ();
  581. }
  582. public Attached Value { get; set; }
  583. }
  584. public class AttachedWrapper2
  585. {
  586. public static readonly AttachableMemberIdentifier FooIdentifier = new AttachableMemberIdentifier (typeof (AttachedWrapper2), "Foo");
  587. static AttachedPropertyStore store = new AttachedPropertyStore ();
  588. public static string GetFoo (object target)
  589. {
  590. object v;
  591. return store.TryGetProperty (FooIdentifier, out v) ? (string) v : null;
  592. }
  593. public static void SetFoo (object target, string value)
  594. {
  595. store.SetProperty (FooIdentifier, value);
  596. }
  597. public static int PropertyCount {
  598. get { return store.PropertyCount; }
  599. }
  600. public AttachedWrapper2 ()
  601. {
  602. Value = new Attached ();
  603. }
  604. public Attached Value { get; set; }
  605. }
  606. public class Attached : Attachable
  607. {
  608. }
  609. public class Attached2
  610. {
  611. internal String Property { get; set; }
  612. }
  613. public class AttachedWrapper3
  614. {
  615. public static void SetProperty (Attached2 a, string value)
  616. {
  617. a.Property = value;
  618. }
  619. }
  620. public class EventStore
  621. {
  622. public bool Method1Invoked;
  623. public event EventHandler<EventArgs> Event1;
  624. public event Func<object> Event2;
  625. public object Examine ()
  626. {
  627. if (Event1 != null)
  628. Event1 (this, EventArgs.Empty);
  629. if (Event2 != null)
  630. return Event2 ();
  631. else
  632. return null;
  633. }
  634. public void Method1 ()
  635. {
  636. throw new Exception ();
  637. }
  638. public void Method1 (object o, EventArgs e)
  639. {
  640. Method1Invoked = true;
  641. }
  642. public object Method2 ()
  643. {
  644. return "foo";
  645. }
  646. }
  647. public class EventStore2<TEventArgs> where TEventArgs : EventArgs
  648. {
  649. public bool Method1Invoked;
  650. public event EventHandler<TEventArgs> Event1;
  651. public event Func<object> Event2;
  652. public object Examine ()
  653. {
  654. if (Event1 != null)
  655. Event1 (this, default (TEventArgs));
  656. if (Event2 != null)
  657. return Event2 ();
  658. else
  659. return null;
  660. }
  661. public void Method1 ()
  662. {
  663. throw new Exception ();
  664. }
  665. public void Method1 (object o, EventArgs e)
  666. {
  667. throw new Exception ();
  668. }
  669. public void Method1 (object o, TEventArgs e)
  670. {
  671. Method1Invoked = true;
  672. }
  673. public object Method2 ()
  674. {
  675. return "foo";
  676. }
  677. }
  678. public class AbstractContainer
  679. {
  680. public AbstractObject Value1 { get; set; }
  681. public AbstractObject Value2 { get; set; }
  682. }
  683. public abstract class AbstractObject
  684. {
  685. public abstract string Foo { get; set; }
  686. }
  687. public class DerivedObject : AbstractObject
  688. {
  689. public override string Foo { get; set; }
  690. }
  691. public class ReadOnlyPropertyContainer
  692. {
  693. string foo;
  694. public string Foo {
  695. get { return foo; }
  696. set { foo = Bar = value; }
  697. }
  698. public string Bar { get; private set; }
  699. }
  700. public class EnumContainer
  701. {
  702. public EnumValueType EnumProperty { get; set; }
  703. }
  704. public enum EnumValueType
  705. {
  706. One,
  707. Two,
  708. Three,
  709. Four
  710. }
  711. [ContentProperty ("ListOfItems")]
  712. public class CollectionContentProperty
  713. {
  714. public IList<SimpleClass> ListOfItems { get; set; }
  715. public CollectionContentProperty ()
  716. {
  717. this.ListOfItems = new List<SimpleClass> ();
  718. }
  719. }
  720. [ContentProperty ("ListOfItems")]
  721. public class CollectionContentPropertyX
  722. {
  723. public IList ListOfItems { get; set; }
  724. public CollectionContentPropertyX ()
  725. {
  726. this.ListOfItems = new List<IEnumerable> ();
  727. }
  728. }
  729. public class SimpleClass
  730. {
  731. }
  732. public class NullableContainer
  733. {
  734. public int? TestProp { get; set; }
  735. }
  736. public class DirectListContainer // for such xml that directly contains items in <*.Items> element.
  737. {
  738. public IList<DirectListContent> Items { get; set; }
  739. public DirectListContainer ()
  740. {
  741. this.Items = new List<DirectListContent> ();
  742. }
  743. }
  744. public class DirectListContent
  745. {
  746. public string Value { get; set; }
  747. }
  748. public class DirectDictionaryContainer // for such xml that directly contains items in <*.Items> element.
  749. {
  750. public IDictionary<EnumValueType,int> Items { get; set; }
  751. public DirectDictionaryContainer ()
  752. {
  753. this.Items = new Dictionary<EnumValueType,int> ();
  754. }
  755. }
  756. }
  757. namespace XamlTest
  758. {
  759. public class Configurations : List<Configuration>
  760. {
  761. private Configuration active;
  762. private bool isFrozen;
  763. public Configuration Active {
  764. get { return this.active; }
  765. set {
  766. if (this.isFrozen) {
  767. throw new InvalidOperationException ("The 'Active' configuration can only be changed via modifying the source file (" + this.Source + ").");
  768. }
  769. this.active = value;
  770. }
  771. }
  772. public string Source { get; private set; }
  773. }
  774. public class Configuration
  775. {
  776. public string Version { get; set; }
  777. public string Path { get; set; }
  778. }
  779. }
  780. // see bug #681480
  781. namespace SecondTest
  782. {
  783. public class TypeOtherAssembly
  784. {
  785. [TypeConverter (typeof (NullableUintListConverter))]
  786. public List<uint?> Values { get; set; }
  787. public TypeOtherAssembly ()
  788. {
  789. this.Values = new List<uint?> ();
  790. }
  791. }
  792. public class NullableUintListConverter : CustomTypeConverterBase
  793. {
  794. public override object ConvertFrom (System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
  795. {
  796. string configValue = value as string;
  797. if (string.IsNullOrWhiteSpace (configValue))
  798. return null;
  799. string delimiterStr = ", ";
  800. char [] delimiters = delimiterStr.ToCharArray ();
  801. string [] tokens = configValue.Split (delimiters, StringSplitOptions.RemoveEmptyEntries);
  802. List<uint?> parsedList = new List<uint?> (tokens.Length);
  803. foreach (string token in tokens)
  804. parsedList.Add(uint.Parse(token));
  805. return parsedList;
  806. }
  807. public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  808. {
  809. var v = (List<uint?>) value;
  810. return String.Join (", ", (from i in v select i.ToString ()).ToArray ());
  811. }
  812. }
  813. public class CustomTypeConverterBase : TypeConverter
  814. {
  815. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  816. {
  817. if (sourceType == typeof (string))
  818. {
  819. return true;
  820. }
  821. return base.CanConvertFrom (context, sourceType);
  822. }
  823. }
  824. #region bug #681202
  825. [MarkupExtensionReturnType (typeof (object))]
  826. public class ResourceExtension : MarkupExtension
  827. {
  828. [ConstructorArgument ("key")]
  829. public object Key { get; set; }
  830. public ResourceExtension (object key)
  831. {
  832. this.Key = key;
  833. }
  834. public override object ProvideValue (IServiceProvider serviceProvider)
  835. {
  836. IXamlSchemaContextProvider service = serviceProvider.GetService (typeof (IXamlSchemaContextProvider)) as IXamlSchemaContextProvider;
  837. IAmbientProvider provider = serviceProvider.GetService (typeof (IAmbientProvider)) as IAmbientProvider;
  838. Debug.Assert (provider != null, "The provider should not be null!");
  839. XamlSchemaContext schemaContext = service.SchemaContext;
  840. XamlType[] types = new XamlType [] { schemaContext.GetXamlType (typeof (ResourcesDict)) };
  841. // ResourceDict is marked as Ambient, so the instance current being deserialized should be in this list.
  842. List<AmbientPropertyValue> list = provider.GetAllAmbientValues (null, false, types) as List<AmbientPropertyValue>;
  843. if (list.Count != 1)
  844. throw new Exception ("expected ambient property count == 1 but " + list.Count);
  845. for (int i = 0; i < list.Count; i++) {
  846. AmbientPropertyValue value = list [i];
  847. ResourcesDict dict = value.Value as ResourcesDict;
  848. // For this example, we know that dict should not be null and that it is the only value in list.
  849. object result = dict [this.Key];
  850. return result;
  851. }
  852. return null;
  853. }
  854. }
  855. [UsableDuringInitialization (true), Ambient]
  856. public class ResourcesDict : Dictionary<object, object>
  857. {
  858. }
  859. public class TestObject
  860. {
  861. public TestObject TestProperty { get; set; }
  862. }
  863. #endregion
  864. public class ResourcesDict2 : Dictionary<object, object>
  865. {
  866. }
  867. public class TestObject2
  868. {
  869. public string TestProperty { get; set; }
  870. }
  871. #region bug #683290
  872. [ContentProperty ("Items")]
  873. public class SimpleType
  874. {
  875. public IList<SimpleType> Items { get; set; }
  876. public IList<SimpleType> NonContentItems { get; set; }
  877. public string TestProperty { get; set; }
  878. public SimpleType ()
  879. {
  880. this.Items = new List<SimpleType> ();
  881. this.NonContentItems=new List<SimpleType> ();
  882. }
  883. }
  884. public class ContentPropertyContainer : Dictionary<object, object>
  885. {
  886. }
  887. #endregion
  888. }
  889. #region "xamarin bug #2927"
  890. namespace XamarinBug2927
  891. {
  892. public class RootClass
  893. {
  894. public RootClass ()
  895. {
  896. Child = new MyChildClass ();
  897. }
  898. public bool Invoked;
  899. public ChildClass Child { get; set; }
  900. }
  901. public class MyRootClass : RootClass
  902. {
  903. public void HandleMyEvent (object sender, EventArgs e)
  904. {
  905. Invoked = true;
  906. }
  907. }
  908. public class RootClass2
  909. {
  910. public RootClass2 ()
  911. {
  912. Child = new MyChildClass ();
  913. }
  914. public bool Invoked;
  915. public ChildClass Child { get; set; }
  916. public void HandleMyEvent (object sender, EventArgs e)
  917. {
  918. Invoked = true;
  919. }
  920. }
  921. public class MyRootClass2 : RootClass2
  922. {
  923. }
  924. public class ChildClass
  925. {
  926. public bool Invoked;
  927. public DescendantClass Descendant { get; set; }
  928. }
  929. public class MyChildClass : ChildClass
  930. {
  931. public MyChildClass ()
  932. {
  933. Descendant = new DescendantClass () { Value = "x" };
  934. }
  935. public void HandleMyEvent (object sender, EventArgs e)
  936. {
  937. Invoked = true;
  938. }
  939. }
  940. public class DescendantClass
  941. {
  942. public bool Invoked;
  943. public event EventHandler DoWork;
  944. public string Value { get; set; }
  945. public void Work ()
  946. {
  947. DoWork (this, EventArgs.Empty);
  948. }
  949. public void HandleMyEvent (object sender, EventArgs e)
  950. {
  951. Invoked = true;
  952. }
  953. }
  954. }
  955. #endregion
  956. #region "xamarin bug 3003"
  957. namespace XamarinBug3003
  958. {
  959. public static class TestContext
  960. {
  961. public static StringWriter Writer = new StringWriter ();
  962. public const string XmlInput = @"<Parent xmlns='http://schemas.example.com/test' Title='Parent Title'>
  963. <Child Parent.AssociatedProperty='child 1' Title='Child Title 1'></Child>
  964. <Child Parent.AssociatedProperty='child 2' Title='Child Title 2'></Child>
  965. </Parent>";
  966. // In bug #3003 repro, there is output "Item 'Child' inserted at index 'x'" , but I couldn't get it in the output either on .NET or Mono.
  967. // On the other hand, in the standalone repro case they are in the output either in mono or in .NET. So I just stopped caring about that as it works as expected.
  968. public const string ExpectedResult = @"
  969. Parent Constructed
  970. ISupportInitialize.BeginInit: Parent
  971. XamlObjectWriterSettings.AfterBeginInit: Parent
  972. XamlObjectWriterSettings.BeforeProperties: Parent
  973. XamlObjectWriterSettings.XamlSetValue: Parent Title, Member: Title
  974. Parent.Title_set: Parent
  975. Child Constructed
  976. ISupportInitialize.BeginInit: Child
  977. XamlObjectWriterSettings.AfterBeginInit: Child
  978. XamlObjectWriterSettings.BeforeProperties: Child
  979. XamlObjectWriterSettings.XamlSetValue: child 1, Member: AssociatedProperty
  980. Parent.SetAssociatedProperty: child 1
  981. XamlObjectWriterSettings.XamlSetValue: Child Title 1, Member: Title
  982. Child.Title_set: Child
  983. XamlObjectWriterSettings.AfterProperties: Child
  984. ISupportInitialize.EndInit: Child
  985. XamlObjectWriterSettings.AfterEndInit: Child
  986. Child Constructed
  987. ISupportInitialize.BeginInit: Child
  988. XamlObjectWriterSettings.AfterBeginInit: Child
  989. XamlObjectWriterSettings.BeforeProperties: Child
  990. XamlObjectWriterSettings.XamlSetValue: child 2, Member: AssociatedProperty
  991. Parent.SetAssociatedProperty: child 2
  992. XamlObjectWriterSettings.XamlSetValue: Child Title 2, Member: Title
  993. Child.Title_set: Child
  994. XamlObjectWriterSettings.AfterProperties: Child
  995. ISupportInitialize.EndInit: Child
  996. XamlObjectWriterSettings.AfterEndInit: Child
  997. XamlObjectWriterSettings.AfterProperties: Parent
  998. ISupportInitialize.EndInit: Parent
  999. XamlObjectWriterSettings.AfterEndInit: Parent
  1000. Loaded Parent
  1001. ";
  1002. }
  1003. public class BaseItemCollection : Collection<BaseItem>
  1004. {
  1005. protected override void InsertItem (int index, BaseItem item)
  1006. {
  1007. base.InsertItem (index, item);
  1008. Console.WriteLine ("Item '{0}' inserted at index '{1}'", item, index);
  1009. }
  1010. }
  1011. public class BaseItem : ISupportInitialize
  1012. {
  1013. Dictionary<string, object> properties = new Dictionary<string, object> ();
  1014. public Dictionary<string, object> Properties
  1015. {
  1016. get { return properties; }
  1017. }
  1018. string title;
  1019. public string Title
  1020. {
  1021. get { return title; }
  1022. set
  1023. {
  1024. title = value;
  1025. TestContext.Writer.WriteLine ("{0}.Title_set: {0}", this.GetType ().Name, value);
  1026. }
  1027. }
  1028. public BaseItem ()
  1029. {
  1030. TestContext.Writer.WriteLine ("{0} Constructed", this.GetType ().Name);
  1031. }
  1032. public void BeginInit ()
  1033. {
  1034. TestContext.Writer.WriteLine ("ISupportInitialize.BeginInit: {0}", this);
  1035. }
  1036. public void EndInit ()
  1037. {
  1038. TestContext.Writer.WriteLine ("ISupportInitialize.EndInit: {0}", this);
  1039. }
  1040. public override string ToString ()
  1041. {
  1042. return this.GetType ().Name.ToString ();
  1043. }
  1044. }
  1045. public class Child : BaseItem
  1046. {
  1047. }
  1048. [ContentProperty ("Children")]
  1049. public class Parent : BaseItem
  1050. {
  1051. BaseItemCollection children = new BaseItemCollection ();
  1052. public BaseItemCollection Children
  1053. {
  1054. get { return children; }
  1055. }
  1056. public static string GetAssociatedProperty (Child child)
  1057. {
  1058. object value;
  1059. if (child.Properties.TryGetValue ("myassociatedproperty", out value)) return value as string;
  1060. return null;
  1061. }
  1062. public static void SetAssociatedProperty (Child child, string value)
  1063. {
  1064. TestContext.Writer.WriteLine ("Parent.SetAssociatedProperty: {0}", value);
  1065. child.Properties ["myassociatedproperty"] = value;
  1066. }
  1067. }
  1068. }
  1069. #endregion