Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Components / DictionaryAdapter / Castle.Components.DictionaryAdapter.Tests / DictionaryAdapterFactoryTestCase.cs
blob05312979f5aab76958e868f3e235ad9574bdf336
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
16 namespace Castle.Components.DictionaryAdapter.Tests
18 using System;
19 using System.Collections;
20 using System.Collections.Generic;
21 using System.ComponentModel;
22 using System.Globalization;
23 using NUnit.Framework;
25 [TestFixture]
26 public class DictionaryAdapterFactoryTestCase
28 private IDictionary dictionary;
29 private DictionaryAdapterFactory factory;
31 [SetUp]
32 public void SetUp()
34 dictionary = new Hashtable();
35 factory = new DictionaryAdapterFactory();
38 [Test]
39 public void CreateAdapter_NoPrefixPropertiesOnly_WorksFine()
41 IPerson person = factory.GetAdapter<IPerson>(dictionary);
42 Assert.IsNotNull(person);
45 [Test, ExpectedException(typeof(TypeLoadException))]
46 public void CreateAdapter_NoPrefixWithMethod_ThrowsException()
48 factory.GetAdapter<IPersonWithMethod>(dictionary);
51 [Test]
52 public void CreateAdapter_PrefixPropertiesOnly_WorksFine()
54 IPerson person = factory.GetAdapter<IPersonWithPrefix>(dictionary);
55 Assert.IsNotNull(person);
58 [Test]
59 public void UpdateAdapter_NoPrefix_UpdatesDictionary()
61 IPerson person = factory.GetAdapter<IPerson>(dictionary);
62 person.Name = "Craig";
63 person.Age = 37;
64 person.DOB = new DateTime(1970, 7, 19);
65 person.Friends = new List<IPerson>(); // I need some friends
67 Assert.AreEqual("Craig", dictionary["Name"]);
68 Assert.AreEqual(37, dictionary["Age"]);
69 Assert.AreEqual(new DateTime(1970, 7, 19), dictionary["DOB"]);
70 Assert.AreEqual(0, ((IList<IPerson>) dictionary["Friends"]).Count);
73 [Test]
74 public void UpdateAdapter_Prefix_UpdatesDictionary()
76 IPerson person = factory.GetAdapter<IPersonWithPrefix>(dictionary);
77 person.Name = "Craig";
78 person.Age = 37;
79 person.DOB = new DateTime(1970, 7, 19);
80 person.Friends = new List<IPerson>();
81 Assert.AreEqual("Craig", dictionary["Name"]);
82 Assert.AreEqual(37, dictionary["Age"]);
83 Assert.AreEqual(new DateTime(1970, 7, 19), dictionary["DOB"]);
84 Assert.AreEqual(0, ((IList<IPerson>) dictionary["Friends"]).Count);
87 [Test]
88 public void UpdateAdapterAndRead_NoPrefix_Matches()
90 IPerson person = factory.GetAdapter<IPerson>(dictionary);
91 person.Name = "Craig";
92 person.Age = 37;
93 person.DOB = new DateTime(1970, 7, 19);
94 person.Friends = new List<IPerson>();
96 Assert.AreEqual("Craig", person.Name);
97 Assert.AreEqual(37, person.Age);
98 Assert.AreEqual(new DateTime(1970, 7, 19), person.DOB);
99 Assert.AreEqual(0, person.Friends.Count);
102 [Test]
103 public void UpdateAdapterAndRead_Prefix_Matches()
105 IPerson person = factory.GetAdapter<IPersonWithPrefix>(dictionary);
106 person.Name = "Craig";
107 person.Age = 37;
108 person.DOB = new DateTime(1970, 7, 19);
109 person.Friends = new List<IPerson>();
111 Assert.AreEqual("Craig", person.Name);
112 Assert.AreEqual(37, person.Age);
113 Assert.AreEqual(new DateTime(1970, 7, 19), person.DOB);
114 Assert.AreEqual(0, person.Friends.Count);
117 [Test]
118 public void UpdateAdapterAndRead_PrefixOverride_Matches()
120 IPerson person = factory.GetAdapter<IPersonWithPrefixOverride>(dictionary);
121 person.Name = "Craig";
123 Assert.AreEqual("Craig", dictionary["Name"]);
126 [Test]
127 public void UpdateAdapterAndRead_TypePrefix_Matches()
129 IPersonWithTypePrefixOverride person =
130 factory.GetAdapter<IPersonWithTypePrefixOverride>(dictionary);
131 person.Height = 72;
133 Assert.AreEqual(72, person.Height);
134 Assert.AreEqual(72, dictionary["Castle.Components.DictionaryAdapter.Tests.IPersonWithTypePrefixOverride#Height"]);
137 [Test]
138 public void ReadAdapter_NoPrefixUnitialized_ReturnsDefaults()
140 IPerson person = factory.GetAdapter<IPerson>(dictionary);
142 Assert.AreEqual(default(string), person.Name);
143 Assert.AreEqual(default(int), person.Age);
144 Assert.AreEqual(default(DateTime), person.DOB);
145 Assert.AreEqual(default(IList<IPerson>), person.Friends);
148 [Test]
149 public void ReadAdapter_PrefixUnitialized_ReturnsDefaults()
151 IPerson person = factory.GetAdapter<IPersonWithPrefix>(dictionary);
153 Assert.AreEqual(default(string), person.Name);
154 Assert.AreEqual(default(int), person.Age);
155 Assert.AreEqual(default(DateTime), person.DOB);
156 Assert.AreEqual(default(IList<IPerson>), person.Friends);
159 [Test]
160 public void UpdateAdapterAndRead_WithSeveralDifferentOverridesWithDifferentPrefixes_DictionaryKeysHaveCorrectPrefixes()
162 IPersonWithDeniedInheritancePrefix person = factory.GetAdapter<IPersonWithDeniedInheritancePrefix>(dictionary);
164 string name = "Ming The Merciless";
165 int numberOfFeet = 2;
166 string hairColour = "Muddy Golden Labrador";
167 string eyeColour = "The Colour Of Broken Dreams";
168 int numberOfHeads = 1;
169 int numberOfFingers = 3;
171 person.Name = name;
172 person.NumberOfFeet = numberOfFeet;
173 person.HairColour = hairColour;
174 person.EyeColour = eyeColour;
175 person.NumberOfHeads = numberOfHeads;
176 person.NumberOfFingers = numberOfFingers;
178 string[] keys = new string[dictionary.Keys.Count];
179 dictionary.Keys.CopyTo(keys, 0);
181 Assert.IsTrue(Array.Exists(keys, delegate(string key) { return key == "Name"; }));
182 Assert.IsTrue(Array.Exists(keys, delegate(string key) { return key == "NumberOfFeet"; }));
183 Assert.IsTrue(Array.Exists(keys, delegate(string key) { return key == "Person_HairColour"; }));
184 Assert.IsTrue(Array.Exists(keys, delegate(string key) { return key == "Person2_Eye__Colour"; }));
185 Assert.IsTrue(Array.Exists(keys, delegate(string key) { return key == "Person2_NumberOfHeads"; }));
186 Assert.IsTrue(Array.Exists(keys, delegate(string key) { return key == "NumberOfFingers"; }));
188 Assert.AreEqual(name, person.Name);
189 Assert.AreEqual(numberOfFeet, person.NumberOfFeet);
190 Assert.AreEqual(hairColour, person.HairColour);
191 Assert.AreEqual(eyeColour, person.EyeColour);
192 Assert.AreEqual(numberOfHeads, person.NumberOfHeads);
193 Assert.AreEqual(numberOfFingers, person.NumberOfFingers);
196 [Test]
197 public void CreateAdapter_WithSubstitionOnProperty_WorksFine()
199 IPerson person = factory.GetAdapter<IPerson>(dictionary);
200 person.First_Name = "Craig";
201 Assert.AreEqual("Craig", dictionary["First Name"]);
204 [Test]
205 public void CreateAdapter_WithSubstitionOnInterface_WorksFine()
207 IPersonWithDeniedInheritancePrefix person = factory.GetAdapter<IPersonWithDeniedInheritancePrefix>(dictionary);
208 person.Max_Width = 22;
209 Assert.AreEqual(22, dictionary["Max Width"]);
212 [Test]
213 public void CreateAdapter_WithComponent_WorksFine()
215 IPerson person = factory.GetAdapter<IPerson>(dictionary);
216 IAddress mailing = person.HomeAddress;
217 Assert.IsNotNull(mailing);
220 [Test]
221 public void ReadAdapter_WithComponent_WorksFine()
223 dictionary["HomeAddress_Line1"] = "77 Lynwood Dr";
224 dictionary["HomeAddress_City"] = "Massapequa";
225 dictionary["HomeAddress_State"] = "NY";
226 dictionary["HomeAddress_ZipCode"] = "11288";
228 IPerson person = factory.GetAdapter<IPerson>(dictionary);
229 IAddress home = person.HomeAddress;
231 Assert.AreEqual(dictionary["HomeAddress_Line1"], home.Line1);
232 Assert.AreEqual(dictionary["HomeAddress_City"], home.City);
233 Assert.AreEqual(dictionary["HomeAddress_State"], home.State);
234 Assert.AreEqual(dictionary["HomeAddress_ZipCode"], home.ZipCode);
237 [Test]
238 public void UpdateAdapter_WithComponent_WorksFine()
240 IPerson person = factory.GetAdapter<IPerson>(dictionary);
241 IAddress home = person.HomeAddress;
242 home.Line1 = "77 Lynwood Dr";
243 home.City = "Massapequa";
244 home.State = "NY";
245 home.ZipCode = "11288";
247 Assert.AreEqual("77 Lynwood Dr", home.Line1);
248 Assert.AreEqual("Massapequa", home.City);
249 Assert.AreEqual("NY", home.State);
250 Assert.AreEqual("11288", home.ZipCode);
253 [Test]
254 public void ReadAdapter_WithComponentOverrideNoPrefix_WorksFine()
256 dictionary["Line1"] = "139 Dartbrook";
257 dictionary["City"] = "Plano";
258 dictionary["State"] = "TX";
259 dictionary["ZipCode"] = "75062";
261 IPerson person = factory.GetAdapter<IPerson>(dictionary);
262 IAddress work = person.WorkAddress;
264 Assert.AreEqual(dictionary["Line1"], work.Line1);
265 Assert.AreEqual(dictionary["City"], work.City);
266 Assert.AreEqual(dictionary["State"], work.State);
267 Assert.AreEqual(dictionary["ZipCode"], work.ZipCode);
270 [Test]
271 public void UpdateAdapter_WithComponentOverrideNoPrefix_WorksFine()
273 IPerson person = factory.GetAdapter<IPerson>(dictionary);
274 IAddress work = person.WorkAddress;
275 work.Line1 = "139 Dartbrook";
276 work.City = "Plano";
277 work.State = "TX";
278 work.ZipCode = "75062";
280 Assert.AreEqual("139 Dartbrook", work.Line1);
281 Assert.AreEqual("Plano", work.City);
282 Assert.AreEqual("TX", work.State);
283 Assert.AreEqual("75062", work.ZipCode);
286 [Test]
287 public void ReadAdapter_WithComponentOverridePrefix_WorksFine()
289 dictionary["Billing_Line1"] = "64 Country Rd";
290 dictionary["Billing_City"] = "Miami";
291 dictionary["Billing_State"] = "FL";
292 dictionary["Billing_ZipCode"] = "33101";
294 IPerson person = factory.GetAdapter<IPerson>(dictionary);
295 IAddress billing = person.BillingAddress;
297 Assert.AreEqual(dictionary["Billing_Line1"], billing.Line1);
298 Assert.AreEqual(dictionary["Billing_City"], billing.City);
299 Assert.AreEqual(dictionary["Billing_State"], billing.State);
300 Assert.AreEqual(dictionary["Billing_ZipCode"], billing.ZipCode);
303 [Test]
304 public void UpdateAdapter_WithComponentOverridePrefix_WorksFine()
306 dictionary["Billing_Line1"] = "64 Country Rd";
307 dictionary["Billing_City"] = "Miami";
308 dictionary["Billing_State"] = "FL";
309 dictionary["Billing_ZipCode"] = "33101";
311 IPerson person = factory.GetAdapter<IPerson>(dictionary);
312 IAddress billing = person.BillingAddress;
313 billing.Line1 = "64 Country Rd";
314 billing.City = "Miami";
315 billing.State = "FL";
316 billing.ZipCode = "33101";
319 [Test]
320 public void CreateAdapter_WithNestedComponent_WorksFine()
322 IPerson person = factory.GetAdapter<IPerson>(dictionary);
323 IAddress mailing = person.HomeAddress;
324 Assert.IsNotNull(mailing.Phone);
327 [Test]
328 public void ReadAdapter_WithNestedComponent_WorksFine()
330 dictionary["HomeAddress_Phone_Number"] = "212-353-1244";
331 dictionary["HomeAddress_Phone_Extension"] = "245";
333 IPerson person = factory.GetAdapter<IPerson>(dictionary);
334 IPhone phone = person.HomeAddress.Phone;
336 Assert.AreEqual(dictionary["HomeAddress_Phone_Number"], phone.Number);
337 Assert.AreEqual(dictionary["HomeAddress_Phone_Extension"], phone.Extension);
340 [Test]
341 public void UpdateAdapter_WithNestedComponent_WorksFine()
343 IPerson person = factory.GetAdapter<IPerson>(dictionary);
344 IPhone phone = person.HomeAddress.Phone;
345 phone.Number = "212-353-1244";
346 phone.Extension = "245";
348 Assert.AreEqual("212-353-1244", phone.Number);
349 Assert.AreEqual("245", phone.Extension);
352 [Test]
353 public void ReadAdapter_WithNestedComponentOverrideNoPrefix_WorksFine()
355 dictionary["Number"] = "972-324-9821";
356 dictionary["Extension"] = "300";
358 IPerson person = factory.GetAdapter<IPerson>(dictionary);
359 IPhone phone = person.WorkAddress.Mobile;
361 Assert.AreEqual(dictionary["Number"], phone.Number);
362 Assert.AreEqual(dictionary["Extension"], phone.Extension);
365 [Test]
366 public void UpdateAdapter_WithNestedComponentOverrideNoPrefix_WorksFine()
368 IPerson person = factory.GetAdapter<IPerson>(dictionary);
369 IPhone phone = person.HomeAddress.Mobile;
370 phone.Number = "972-324-9821";
371 phone.Extension = "300";
373 Assert.AreEqual("972-324-9821", phone.Number);
374 Assert.AreEqual("300", phone.Extension);
377 [Test]
378 public void ReadAdapter_WithNestedComponentOverridePrefix_WorksFine()
380 dictionary["HomeAddress_Emr_Number"] = "911";
382 IPerson person = factory.GetAdapter<IPerson>(dictionary);
383 IPhone phone = person.HomeAddress.Emergency;
385 Assert.AreEqual(dictionary["HomeAddress_Emr_Number"], phone.Number);
386 Assert.IsNull(dictionary["HomeAddress_Emr_Extension"]);
389 [Test]
390 public void ReadAdapter_WithDefaultConversions_WorksFine()
392 DateTime now = DateTime.Now;
393 Guid guid = Guid.NewGuid();
395 dictionary["Int"] = string.Format("{0}", 22);
396 dictionary["Float"] = string.Format("{0}", 98.6);
397 dictionary["Double"] = string.Format("{0}", 3.14D);
398 dictionary["Decimal"] = string.Format("{0}", 100M);
399 dictionary["String"] = "Hello World";
400 dictionary["DateTime"] = now.ToShortDateString();
401 dictionary["Guid"] = guid.ToString();
403 IConversions conversions = factory.GetAdapter<IConversions>(dictionary);
404 Assert.AreEqual(22, conversions.Int);
405 Assert.AreEqual(98.6, conversions.Float);
406 Assert.AreEqual(3.14, conversions.Double);
407 Assert.AreEqual(100, conversions.Decimal);
408 Assert.AreEqual("Hello World", conversions.String);
409 Assert.AreEqual(now.Date, conversions.DateTime.Date);
410 Assert.AreEqual(guid, conversions.Guid);
413 [Test]
414 public void UpdateAdapter_WithDefaultConversions_WorksFine()
416 DateTime today = DateTime.Today;
417 Guid guid = Guid.NewGuid();
419 IConversionsToString conversions = factory.GetAdapter<IConversionsToString>(dictionary);
420 conversions.Int = 22;
421 conversions.Float = 98.6F;
422 conversions.Double = 3.14;
423 conversions.Decimal = 100;
424 conversions.DateTime = today;
425 conversions.Guid = guid;
426 conversions.Phone = new Phone("2124751012", "22");
428 Assert.AreEqual(string.Format("{0}", 22), dictionary["Int"]);
429 Assert.AreEqual(string.Format("{0}", 98.6), dictionary["Float"]);
430 Assert.AreEqual(string.Format("{0}", 3.14D), dictionary["Double"]);
431 Assert.AreEqual(string.Format("{0}", 100M), dictionary["Decimal"]);
432 Assert.AreEqual(today.ToShortDateString(), dictionary["DateTime"]);
433 Assert.AreEqual(guid.ToString(), dictionary["Guid"]);
434 Assert.AreEqual("2124751012,22", dictionary["Phone"]);
437 [Test]
438 public void ReadAdapter_WithDefaultNullableConversions_WorksFine()
440 DateTime? now = DateTime.Now;
441 Guid? guid = Guid.NewGuid();
443 dictionary["NullInt"] = string.Format("{0}", 22);
444 dictionary["NullFloat"] = string.Format("{0}", 98.6);
445 dictionary["NullDouble"] = string.Format("{0}", 3.14D);
446 dictionary["NullDecimal"] = string.Format("{0}", 100M);
447 dictionary["NullDateTime"] = now.Value.ToShortDateString();
448 dictionary["NullGuid"] = guid.ToString();
450 IConversions conversions = factory.GetAdapter<IConversions>(dictionary);
451 Assert.AreEqual(22, conversions.NullInt);
452 Assert.AreEqual(98.6, conversions.NullFloat);
453 Assert.AreEqual(3.14, conversions.NullDouble);
454 Assert.AreEqual(100, conversions.NullDecimal);
455 Assert.AreEqual(now.Value.Date, conversions.NullDateTime.Value.Date);
456 Assert.AreEqual(guid, conversions.NullGuid);
459 [Test]
460 public void UpdateAdapter_WithDefaultNullableConversions_WorksFine()
462 DateTime? today = DateTime.Today;
463 Guid? guid = Guid.NewGuid();
465 IConversionsToString conversions = factory.GetAdapter<IConversionsToString>(dictionary);
466 conversions.NullInt = 22;
467 conversions.NullFloat = 98.6F;
468 conversions.NullDouble = 3.14;
469 conversions.NullDecimal = 100;
470 conversions.NullDateTime = today;
471 conversions.NullGuid = guid;
473 Assert.AreEqual(string.Format("{0}", 22), dictionary["NullInt"]);
474 Assert.AreEqual(string.Format("{0}", 98.6), dictionary["NullFloat"]);
475 Assert.AreEqual(string.Format("{0}", 3.14D), dictionary["NullDouble"]);
476 Assert.AreEqual(string.Format("{0}", 100M), dictionary["NullDecimal"]);
477 Assert.AreEqual(today.Value.ToShortDateString(), dictionary["NullDateTime"]);
478 Assert.AreEqual(guid.ToString(), dictionary["NullGuid"]);
481 [Test]
482 public void ReadAdapter_WithDefaultNullConversions_WorksFine()
484 IConversions conversions = factory.GetAdapter<IConversions>(dictionary);
485 Assert.IsNull(conversions.NullInt);
486 Assert.IsNull(conversions.NullFloat);
487 Assert.IsNull(conversions.NullDouble);
488 Assert.IsNull(conversions.NullDecimal);
489 Assert.IsNull(conversions.NullDateTime);
490 Assert.IsNull(conversions.NullGuid);
493 [Test]
494 public void UpdateAdapter_WithDefaultNullConversions_WorksFine()
496 IConversionsToString conversions = factory.GetAdapter<IConversionsToString>(dictionary);
497 conversions.NullInt = null;
498 conversions.NullFloat = null;
499 conversions.NullDecimal = null;
500 conversions.NullDateTime = null;
501 conversions.NullGuid = null;
503 Assert.IsNull(dictionary["NullInt"]);
504 Assert.IsNull(dictionary["NullFloat"]);
505 Assert.IsNull(dictionary["NullDouble"]);
506 Assert.IsNull(dictionary["NullDecimal"]);
507 Assert.IsNull(dictionary["NullDateTime"]);
508 Assert.IsNull(dictionary["NullGuid"]);
511 [Test]
512 public void ReadAdapter_WithStringLists_WorksFine()
514 dictionary["Names"] = "Craig,Brenda,Kaitlyn,Lauren,Matthew";
515 dictionary["Ages"] = "37,36,5,3,1";
517 IStringLists lists = factory.GetAdapter<IStringLists>(dictionary);
519 IList<string> names = lists.Names;
520 Assert.IsNotNull(names);
521 Assert.AreEqual(5, names.Count);
522 Assert.AreEqual("Craig", names[0]);
523 Assert.AreEqual("Brenda", names[1]);
524 Assert.AreEqual("Kaitlyn", names[2]);
525 Assert.AreEqual("Lauren", names[3]);
526 Assert.AreEqual("Matthew", names[4]);
528 IList<int> ages = lists.Ages;
529 Assert.AreEqual(5, ages.Count);
530 Assert.AreEqual(37, ages[0]);
531 Assert.AreEqual(36, ages[1]);
532 Assert.AreEqual(5, ages[2]);
533 Assert.AreEqual(3, ages[3]);
534 Assert.AreEqual(1, ages[4]);
537 [Test]
538 public void UpdateAdapter_WithStringLists_WorksFine()
540 IStringLists lists = factory.GetAdapter<IStringLists>(dictionary);
542 IList<string> names = lists.Names;
543 names.Add("Craig");
544 names.Add("Brenda");
545 names.Add("Kaitlyn");
546 names.Add("Lauren");
547 names.Add("Matthew");
549 Assert.AreEqual("Craig,Brenda,Kaitlyn,Lauren,Matthew", dictionary["Names"]);
551 IList<int> ages = new List<int>();
552 ages.Add(37);
553 ages.Add(36);
554 ages.Add(5);
555 ages.Add(3);
556 ages.Add(1);
557 lists.Ages = ages;
559 Assert.AreEqual("37,36,5,3,1", dictionary["Ages"]);
563 public interface IPhone
565 string Number { get; set; }
566 string Extension { get; set; }
569 public class Phone : IPhone
571 private string number;
572 private string extension;
574 public Phone()
578 public Phone(string number, string extension)
580 this.number = number;
581 this.extension = extension;
584 public string Extension
586 get { return extension; }
587 set { extension = value; }
590 public string Number
592 get { return number; }
593 set { number = value; }
597 public interface IAddress
599 string Line1 { get; set; }
600 string Line2 { get; set; }
601 string City { get; set; }
602 string State { get; set; }
603 string ZipCode { get; set; }
605 [DictionaryComponent]
606 IPhone Phone { get; }
608 [DictionaryComponent(NoPrefix = true)]
609 IPhone Mobile { get; }
611 [DictionaryComponent(Prefix = "Emr_")]
612 IPhone Emergency { get; }
615 public class Address : IAddress
617 private string line1;
618 private string line2;
619 private string city;
620 private string state;
621 private string zipCode;
622 private IPhone phone;
623 private IPhone mobile;
624 private IPhone emergency;
626 public string Line1
628 get { return line1; }
629 set { line1 = value; }
632 public string Line2
634 get { return line2; }
635 set { line2 = value; }
638 public string City
640 get { return city; }
641 set { city = value; }
644 public string State
646 get { return state; }
647 set { state = value; }
650 public string ZipCode
652 get { return zipCode; }
653 set { zipCode = value; }
656 public IPhone Phone
660 if (phone == null)
662 phone = new Phone();
664 return phone;
668 public IPhone Mobile
672 if (mobile == null)
674 mobile = new Phone();
676 return mobile;
681 public IPhone Emergency
685 if (emergency == null)
687 emergency = new Phone();
689 return emergency;
694 public interface IPerson
696 string Name { get; set; }
698 int Age { get; set; }
700 DateTime DOB { get; set; }
702 IList<IPerson> Friends { get; set; }
704 [DictionaryKeySubstitution("_", " ")]
705 string First_Name { get; set; }
707 [DictionaryComponent]
708 IAddress HomeAddress { get; set; }
710 [DictionaryComponent(NoPrefix = true)]
711 IAddress WorkAddress { get; set; }
713 [DictionaryComponent(Prefix = "Billing_")]
714 IAddress BillingAddress { get; set; }
717 public interface IPersonWithoutPrefix : IPerson
719 int NumberOfFeet { get; set; }
722 [DictionaryKeyPrefix("Person_")]
723 public interface IPersonWithPrefix : IPersonWithoutPrefix
725 string HairColour { get; set; }
728 [DictionaryKeyPrefix("Person2_")]
729 public interface IPersonWithPrefixOverride : IPersonWithPrefix
731 [DictionaryKey("Eye__Colour")]
732 string EyeColour { get; set; }
735 [DictionaryTypeKeyPrefix]
736 public interface IPersonWithTypePrefixOverride : IPersonWithPrefixOverride
738 int Height { get; set; }
741 public interface IPersonWithPrefixOverrideFurtherOverride : IPersonWithPrefixOverride
743 int NumberOfHeads { get; set; }
746 [DictionaryKeyPrefix("")]
747 [DictionaryKeySubstitution("_", " ")]
748 public interface IPersonWithDeniedInheritancePrefix : IPersonWithPrefixOverrideFurtherOverride
750 int NumberOfFingers { get; set; }
752 int Max_Width { get; set; }
755 public interface IPersonWithMethod : IPerson
757 void Run();
760 public interface IConversions
762 int Int { get; set; }
763 float Float { get; set; }
764 double Double { get; set; }
765 decimal Decimal { get; set; }
766 String String { get; set; }
767 DateTime DateTime { get; set; }
768 Guid Guid { get; set; }
769 int? NullInt { get; set; }
770 float? NullFloat { get; set; }
771 double? NullDouble { get; set; }
772 DateTime? NullDateTime { get; set; }
773 Guid? NullGuid { get; set; }
774 decimal? NullDecimal { get; set; }
777 [DictionaryStringValues]
778 public interface IConversionsToString : IConversions
780 [TypeConverter(typeof(PhoneConverter))]
781 Phone Phone { get; set; }
784 public interface IStringLists
786 [DictionaryStringList]
787 IList<string> Names { get; }
789 [DictionaryStringList]
790 IList<int> Ages { get; set; }
793 public class PhoneConverter : TypeConverter
795 public override bool CanConvertFrom(ITypeDescriptorContext context,
796 Type sourceType)
798 if (sourceType == typeof(string))
800 return true;
802 return base.CanConvertFrom(context, sourceType);
805 public override object ConvertFrom(ITypeDescriptorContext context,
806 CultureInfo culture, object value)
808 if (value is string)
810 string[] fields = ((string) value).Split(new char[] {','});
811 return new Phone(fields[0], fields[1]);
813 return base.ConvertFrom(context, culture, value);
816 public override object ConvertTo(ITypeDescriptorContext context,
817 CultureInfo culture, object value, Type destinationType)
819 if (destinationType == typeof(string))
821 return ((Phone) value).Number + "," + ((Phone) value).Extension;
823 return base.ConvertTo(context, culture, value, destinationType);