Added RedirectUsingNamedRoute
[castle.git] / Components / DictionaryAdapter / Castle.Components.DictionaryAdapter.Tests / DictionaryAdapterFactoryTestCase.cs
blobbd7536690bf0903dbe4d16b53e3d973cb22ecef7
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 ReadAdapter_NoPrefixUnitialized_ReturnsDefaults()
129 IPerson person = factory.GetAdapter<IPerson>(dictionary);
131 Assert.AreEqual(default(string), person.Name);
132 Assert.AreEqual(default(int), person.Age);
133 Assert.AreEqual(default(DateTime), person.DOB);
134 Assert.AreEqual(default(IList<IPerson>), person.Friends);
137 [Test]
138 public void ReadAdapter_PrefixUnitialized_ReturnsDefaults()
140 IPerson person = factory.GetAdapter<IPersonWithPrefix>(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 UpdateAdapterAndRead_WithSeveralDifferentOverridesWithDifferentPrefixes_DictionaryKeysHaveCorrectPrefixes()
151 IPersonWithDeniedInheritancePrefix person = factory.GetAdapter<IPersonWithDeniedInheritancePrefix>(dictionary);
153 string name = "Ming The Merciless";
154 int numberOfFeet = 2;
155 string hairColour = "Muddy Golden Labrador";
156 string eyeColour = "The Colour Of Broken Dreams";
157 int numberOfHeads = 1;
158 int numberOfFingers = 3;
160 person.Name = name;
161 person.NumberOfFeet = numberOfFeet;
162 person.HairColour = hairColour;
163 person.EyeColour = eyeColour;
164 person.NumberOfHeads = numberOfHeads;
165 person.NumberOfFingers = numberOfFingers;
167 string[] keys = new string[dictionary.Keys.Count];
168 dictionary.Keys.CopyTo(keys, 0);
170 Assert.IsTrue(Array.Exists(keys, delegate(string key) { return key == "Name"; }));
171 Assert.IsTrue(Array.Exists(keys, delegate(string key) { return key == "NumberOfFeet"; }));
172 Assert.IsTrue(Array.Exists(keys, delegate(string key) { return key == "Person_HairColour"; }));
173 Assert.IsTrue(Array.Exists(keys, delegate(string key) { return key == "Person2_Eye__Colour"; }));
174 Assert.IsTrue(Array.Exists(keys, delegate(string key) { return key == "Person2_NumberOfHeads"; }));
175 Assert.IsTrue(Array.Exists(keys, delegate(string key) { return key == "NumberOfFingers"; }));
177 Assert.AreEqual(name, person.Name);
178 Assert.AreEqual(numberOfFeet, person.NumberOfFeet);
179 Assert.AreEqual(hairColour, person.HairColour);
180 Assert.AreEqual(eyeColour, person.EyeColour);
181 Assert.AreEqual(numberOfHeads, person.NumberOfHeads);
182 Assert.AreEqual(numberOfFingers, person.NumberOfFingers);
185 [Test]
186 public void CreateAdapter_WithSubstitionOnProperty_WorksFine()
188 IPerson person = factory.GetAdapter<IPerson>(dictionary);
189 person.First_Name = "Craig";
190 Assert.AreEqual("Craig", dictionary["First Name"]);
193 [Test]
194 public void CreateAdapter_WithSubstitionOnInterface_WorksFine()
196 IPersonWithDeniedInheritancePrefix person = factory.GetAdapter<IPersonWithDeniedInheritancePrefix>(dictionary);
197 person.Max_Width = 22;
198 Assert.AreEqual(22, dictionary["Max Width"]);
201 [Test]
202 public void CreateAdapter_WithComponent_WorksFine()
204 IPerson person = factory.GetAdapter<IPerson>(dictionary);
205 IAddress mailing = person.HomeAddress;
206 Assert.IsNotNull(mailing);
209 [Test]
210 public void ReadAdapter_WithComponent_WorksFine()
212 dictionary["HomeAddress_Line1"] = "77 Lynwood Dr";
213 dictionary["HomeAddress_City"] = "Massapequa";
214 dictionary["HomeAddress_State"] = "NY";
215 dictionary["HomeAddress_ZipCode"] = "11288";
217 IPerson person = factory.GetAdapter<IPerson>(dictionary);
218 IAddress home = person.HomeAddress;
220 Assert.AreEqual(dictionary["HomeAddress_Line1"], home.Line1);
221 Assert.AreEqual(dictionary["HomeAddress_City"], home.City);
222 Assert.AreEqual(dictionary["HomeAddress_State"], home.State);
223 Assert.AreEqual(dictionary["HomeAddress_ZipCode"], home.ZipCode);
226 [Test]
227 public void UpdateAdapter_WithComponent_WorksFine()
229 IPerson person = factory.GetAdapter<IPerson>(dictionary);
230 IAddress home = person.HomeAddress;
231 home.Line1 = "77 Lynwood Dr";
232 home.City = "Massapequa";
233 home.State = "NY";
234 home.ZipCode = "11288";
236 Assert.AreEqual("77 Lynwood Dr", home.Line1);
237 Assert.AreEqual("Massapequa", home.City);
238 Assert.AreEqual("NY", home.State);
239 Assert.AreEqual("11288", home.ZipCode);
242 [Test]
243 public void ReadAdapter_WithComponentOverrideNoPrefix_WorksFine()
245 dictionary["Line1"] = "139 Dartbrook";
246 dictionary["City"] = "Plano";
247 dictionary["State"] = "TX";
248 dictionary["ZipCode"] = "75062";
250 IPerson person = factory.GetAdapter<IPerson>(dictionary);
251 IAddress work = person.WorkAddress;
253 Assert.AreEqual(dictionary["Line1"], work.Line1);
254 Assert.AreEqual(dictionary["City"], work.City);
255 Assert.AreEqual(dictionary["State"], work.State);
256 Assert.AreEqual(dictionary["ZipCode"], work.ZipCode);
259 [Test]
260 public void UpdateAdapter_WithComponentOverrideNoPrefix_WorksFine()
262 IPerson person = factory.GetAdapter<IPerson>(dictionary);
263 IAddress work = person.WorkAddress;
264 work.Line1 = "139 Dartbrook";
265 work.City = "Plano";
266 work.State = "TX";
267 work.ZipCode = "75062";
269 Assert.AreEqual("139 Dartbrook", work.Line1);
270 Assert.AreEqual("Plano", work.City);
271 Assert.AreEqual("TX", work.State);
272 Assert.AreEqual("75062", work.ZipCode);
275 [Test]
276 public void ReadAdapter_WithComponentOverridePrefix_WorksFine()
278 dictionary["Billing_Line1"] = "64 Country Rd";
279 dictionary["Billing_City"] = "Miami";
280 dictionary["Billing_State"] = "FL";
281 dictionary["Billing_ZipCode"] = "33101";
283 IPerson person = factory.GetAdapter<IPerson>(dictionary);
284 IAddress billing = person.BillingAddress;
286 Assert.AreEqual(dictionary["Billing_Line1"], billing.Line1);
287 Assert.AreEqual(dictionary["Billing_City"], billing.City);
288 Assert.AreEqual(dictionary["Billing_State"], billing.State);
289 Assert.AreEqual(dictionary["Billing_ZipCode"], billing.ZipCode);
292 [Test]
293 public void UpdateAdapter_WithComponentOverridePrefix_WorksFine()
295 dictionary["Billing_Line1"] = "64 Country Rd";
296 dictionary["Billing_City"] = "Miami";
297 dictionary["Billing_State"] = "FL";
298 dictionary["Billing_ZipCode"] = "33101";
300 IPerson person = factory.GetAdapter<IPerson>(dictionary);
301 IAddress billing = person.BillingAddress;
302 billing.Line1 = "64 Country Rd";
303 billing.City = "Miami";
304 billing.State = "FL";
305 billing.ZipCode = "33101";
308 [Test]
309 public void CreateAdapter_WithNestedComponent_WorksFine()
311 IPerson person = factory.GetAdapter<IPerson>(dictionary);
312 IAddress mailing = person.HomeAddress;
313 Assert.IsNotNull(mailing.Phone);
316 [Test]
317 public void ReadAdapter_WithNestedComponent_WorksFine()
319 dictionary["HomeAddress_Phone_Number"] = "212-353-1244";
320 dictionary["HomeAddress_Phone_Extension"] = "245";
322 IPerson person = factory.GetAdapter<IPerson>(dictionary);
323 IPhone phone = person.HomeAddress.Phone;
325 Assert.AreEqual(dictionary["HomeAddress_Phone_Number"], phone.Number);
326 Assert.AreEqual(dictionary["HomeAddress_Phone_Extension"], phone.Extension);
329 [Test]
330 public void UpdateAdapter_WithNestedComponent_WorksFine()
332 IPerson person = factory.GetAdapter<IPerson>(dictionary);
333 IPhone phone = person.HomeAddress.Phone;
334 phone.Number = "212-353-1244";
335 phone.Extension = "245";
337 Assert.AreEqual("212-353-1244", phone.Number);
338 Assert.AreEqual("245", phone.Extension);
341 [Test]
342 public void ReadAdapter_WithNestedComponentOverrideNoPrefix_WorksFine()
344 dictionary["Number"] = "972-324-9821";
345 dictionary["Extension"] = "300";
347 IPerson person = factory.GetAdapter<IPerson>(dictionary);
348 IPhone phone = person.WorkAddress.Mobile;
350 Assert.AreEqual(dictionary["Number"], phone.Number);
351 Assert.AreEqual(dictionary["Extension"], phone.Extension);
354 [Test]
355 public void UpdateAdapter_WithNestedComponentOverrideNoPrefix_WorksFine()
357 IPerson person = factory.GetAdapter<IPerson>(dictionary);
358 IPhone phone = person.HomeAddress.Mobile;
359 phone.Number = "972-324-9821";
360 phone.Extension = "300";
362 Assert.AreEqual("972-324-9821", phone.Number);
363 Assert.AreEqual("300", phone.Extension);
366 [Test]
367 public void ReadAdapter_WithNestedComponentOverridePrefix_WorksFine()
369 dictionary["HomeAddress_Emr_Number"] = "911";
371 IPerson person = factory.GetAdapter<IPerson>(dictionary);
372 IPhone phone = person.HomeAddress.Emergency;
374 Assert.AreEqual(dictionary["HomeAddress_Emr_Number"], phone.Number);
375 Assert.IsNull(dictionary["HomeAddress_Emr_Extension"]);
378 [Test]
379 public void ReadAdapter_WithDefaultConversions_WorksFine()
381 DateTime now = DateTime.Now;
382 Guid guid = Guid.NewGuid();
384 dictionary["Int"] = string.Format("{0}", 22);
385 dictionary["Float"] = string.Format("{0}", 98.6);
386 dictionary["Double"] = string.Format("{0}", 3.14D);
387 dictionary["Decimal"] = string.Format("{0}", 100M);
388 dictionary["String"] = "Hello World";
389 dictionary["DateTime"] = now.ToShortDateString();
390 dictionary["Guid"] = guid.ToString();
392 IConversions conversions = factory.GetAdapter<IConversions>(dictionary);
393 Assert.AreEqual(22, conversions.Int);
394 Assert.AreEqual(98.6, conversions.Float);
395 Assert.AreEqual(3.14, conversions.Double);
396 Assert.AreEqual(100, conversions.Decimal);
397 Assert.AreEqual("Hello World", conversions.String);
398 Assert.AreEqual(now.Date, conversions.DateTime.Date);
399 Assert.AreEqual(guid, conversions.Guid);
402 [Test]
403 public void UpdateAdapter_WithDefaultConversions_WorksFine()
405 DateTime today = DateTime.Today;
406 Guid guid = Guid.NewGuid();
408 IConversionsToString conversions = factory.GetAdapter<IConversionsToString>(dictionary);
409 conversions.Int = 22;
410 conversions.Float = 98.6F;
411 conversions.Double = 3.14;
412 conversions.Decimal = 100;
413 conversions.DateTime = today;
414 conversions.Guid = guid;
415 conversions.Phone = new Phone("2124751012", "22");
417 Assert.AreEqual(string.Format("{0}", 22), dictionary["Int"]);
418 Assert.AreEqual(string.Format("{0}", 98.6), dictionary["Float"]);
419 Assert.AreEqual(string.Format("{0}", 3.14D), dictionary["Double"]);
420 Assert.AreEqual(string.Format("{0}", 100M), dictionary["Decimal"]);
421 Assert.AreEqual(today.ToShortDateString(), dictionary["DateTime"]);
422 Assert.AreEqual(guid.ToString(), dictionary["Guid"]);
423 Assert.AreEqual("2124751012,22", dictionary["Phone"]);
426 [Test]
427 public void ReadAdapter_WithDefaultNullableConversions_WorksFine()
429 DateTime? now = DateTime.Now;
430 Guid? guid = Guid.NewGuid();
432 dictionary["NullInt"] = string.Format("{0}", 22);
433 dictionary["NullFloat"] = string.Format("{0}", 98.6);
434 dictionary["NullDouble"] = string.Format("{0}", 3.14D);
435 dictionary["NullDecimal"] = string.Format("{0}", 100M);
436 dictionary["NullDateTime"] = now.Value.ToShortDateString();
437 dictionary["NullGuid"] = guid.ToString();
439 IConversions conversions = factory.GetAdapter<IConversions>(dictionary);
440 Assert.AreEqual(22, conversions.NullInt);
441 Assert.AreEqual(98.6, conversions.NullFloat);
442 Assert.AreEqual(3.14, conversions.NullDouble);
443 Assert.AreEqual(100, conversions.NullDecimal);
444 Assert.AreEqual(now.Value.Date, conversions.NullDateTime.Value.Date);
445 Assert.AreEqual(guid, conversions.NullGuid);
448 [Test]
449 public void UpdateAdapter_WithDefaultNullableConversions_WorksFine()
451 DateTime? today = DateTime.Today;
452 Guid? guid = Guid.NewGuid();
454 IConversionsToString conversions = factory.GetAdapter<IConversionsToString>(dictionary);
455 conversions.NullInt = 22;
456 conversions.NullFloat = 98.6F;
457 conversions.NullDouble = 3.14;
458 conversions.NullDecimal = 100;
459 conversions.NullDateTime = today;
460 conversions.NullGuid = guid;
462 Assert.AreEqual(string.Format("{0}", 22), dictionary["NullInt"]);
463 Assert.AreEqual(string.Format("{0}", 98.6), dictionary["NullFloat"]);
464 Assert.AreEqual(string.Format("{0}", 3.14D), dictionary["NullDouble"]);
465 Assert.AreEqual(string.Format("{0}", 100M), dictionary["NullDecimal"]);
466 Assert.AreEqual(today.Value.ToShortDateString(), dictionary["NullDateTime"]);
467 Assert.AreEqual(guid.ToString(), dictionary["NullGuid"]);
470 [Test]
471 public void ReadAdapter_WithDefaultNullConversions_WorksFine()
473 IConversions conversions = factory.GetAdapter<IConversions>(dictionary);
474 Assert.IsNull(conversions.NullInt);
475 Assert.IsNull(conversions.NullFloat);
476 Assert.IsNull(conversions.NullDouble);
477 Assert.IsNull(conversions.NullDecimal);
478 Assert.IsNull(conversions.NullDateTime);
479 Assert.IsNull(conversions.NullGuid);
482 [Test]
483 public void UpdateAdapter_WithDefaultNullConversions_WorksFine()
485 IConversionsToString conversions = factory.GetAdapter<IConversionsToString>(dictionary);
486 conversions.NullInt = null;
487 conversions.NullFloat = null;
488 conversions.NullDecimal = null;
489 conversions.NullDateTime = null;
490 conversions.NullGuid = null;
492 Assert.IsNull(dictionary["NullInt"]);
493 Assert.IsNull(dictionary["NullFloat"]);
494 Assert.IsNull(dictionary["NullDouble"]);
495 Assert.IsNull(dictionary["NullDecimal"]);
496 Assert.IsNull(dictionary["NullDateTime"]);
497 Assert.IsNull(dictionary["NullGuid"]);
500 [Test]
501 public void ReadAdapter_WithStringLists_WorksFine()
503 dictionary["Names"] = "Craig,Brenda,Kaitlyn,Lauren,Matthew";
504 dictionary["Ages"] = "37,36,5,3,1";
506 IStringLists lists = factory.GetAdapter<IStringLists>(dictionary);
508 IList<string> names = lists.Names;
509 Assert.IsNotNull(names);
510 Assert.AreEqual(5, names.Count);
511 Assert.AreEqual("Craig", names[0]);
512 Assert.AreEqual("Brenda", names[1]);
513 Assert.AreEqual("Kaitlyn", names[2]);
514 Assert.AreEqual("Lauren", names[3]);
515 Assert.AreEqual("Matthew", names[4]);
517 IList<int> ages = lists.Ages;
518 Assert.AreEqual(5, ages.Count);
519 Assert.AreEqual(37, ages[0]);
520 Assert.AreEqual(36, ages[1]);
521 Assert.AreEqual(5, ages[2]);
522 Assert.AreEqual(3, ages[3]);
523 Assert.AreEqual(1, ages[4]);
526 [Test]
527 public void UpdateAdapter_WithStringLists_WorksFine()
529 IStringLists lists = factory.GetAdapter<IStringLists>(dictionary);
531 IList<string> names = lists.Names;
532 names.Add("Craig");
533 names.Add("Brenda");
534 names.Add("Kaitlyn");
535 names.Add("Lauren");
536 names.Add("Matthew");
538 Assert.AreEqual("Craig,Brenda,Kaitlyn,Lauren,Matthew", dictionary["Names"]);
540 IList<int> ages = new List<int>();
541 ages.Add(37);
542 ages.Add(36);
543 ages.Add(5);
544 ages.Add(3);
545 ages.Add(1);
546 lists.Ages = ages;
548 Assert.AreEqual("37,36,5,3,1", dictionary["Ages"]);
552 public interface IPhone
554 string Number { get; set; }
555 string Extension { get; set; }
558 public class Phone : IPhone
560 private string number;
561 private string extension;
563 public Phone()
567 public Phone(string number, string extension)
569 this.number = number;
570 this.extension = extension;
573 public string Extension
575 get { return extension; }
576 set { extension = value; }
579 public string Number
581 get { return number; }
582 set { number = value; }
586 public interface IAddress
588 string Line1 { get; set; }
589 string Line2 { get; set; }
590 string City { get; set; }
591 string State { get; set; }
592 string ZipCode { get; set; }
594 [DictionaryComponent]
595 IPhone Phone { get; }
597 [DictionaryComponent(NoPrefix = true)]
598 IPhone Mobile { get; }
600 [DictionaryComponent(Prefix = "Emr_")]
601 IPhone Emergency { get; }
604 public class Address : IAddress
606 private string line1;
607 private string line2;
608 private string city;
609 private string state;
610 private string zipCode;
611 private IPhone phone;
612 private IPhone mobile;
613 private IPhone emergency;
615 public string Line1
617 get { return line1; }
618 set { line1 = value; }
621 public string Line2
623 get { return line2; }
624 set { line2 = value; }
627 public string City
629 get { return city; }
630 set { city = value; }
633 public string State
635 get { return state; }
636 set { state = value; }
639 public string ZipCode
641 get { return zipCode; }
642 set { zipCode = value; }
645 public IPhone Phone
649 if (phone == null)
651 phone = new Phone();
653 return phone;
657 public IPhone Mobile
661 if (mobile == null)
663 mobile = new Phone();
665 return mobile;
670 public IPhone Emergency
674 if (emergency == null)
676 emergency = new Phone();
678 return emergency;
683 public interface IPerson
685 string Name { get; set; }
687 int Age { get; set; }
689 DateTime DOB { get; set; }
691 IList<IPerson> Friends { get; set; }
693 [DictionaryKeySubstitution("_", " ")]
694 string First_Name { get; set; }
696 [DictionaryComponent]
697 IAddress HomeAddress { get; set; }
699 [DictionaryComponent(NoPrefix = true)]
700 IAddress WorkAddress { get; set; }
702 [DictionaryComponent(Prefix = "Billing_")]
703 IAddress BillingAddress { get; set; }
706 public interface IPersonWithoutPrefix : IPerson
708 int NumberOfFeet { get; set; }
711 [DictionaryKeyPrefix("Person_")]
712 public interface IPersonWithPrefix : IPersonWithoutPrefix
714 string HairColour { get; set; }
717 [DictionaryKeyPrefix("Person2_")]
718 public interface IPersonWithPrefixOverride : IPersonWithPrefix
720 [DictionaryKey("Eye__Colour")]
721 string EyeColour { get; set; }
724 public interface IPersonWithPrefixOverrideFurtherOverride : IPersonWithPrefixOverride
726 int NumberOfHeads { get; set; }
729 [DictionaryKeyPrefix("")]
730 [DictionaryKeySubstitution("_", " ")]
731 public interface IPersonWithDeniedInheritancePrefix : IPersonWithPrefixOverrideFurtherOverride
733 int NumberOfFingers { get; set; }
735 int Max_Width { get; set; }
738 public interface IPersonWithMethod : IPerson
740 void Run();
743 public interface IConversions
745 int Int { get; set; }
746 float Float { get; set; }
747 double Double { get; set; }
748 decimal Decimal { get; set; }
749 String String { get; set; }
750 DateTime DateTime { get; set; }
751 Guid Guid { get; set; }
752 int? NullInt { get; set; }
753 float? NullFloat { get; set; }
754 double? NullDouble { get; set; }
755 DateTime? NullDateTime { get; set; }
756 Guid? NullGuid { get; set; }
757 decimal? NullDecimal { get; set; }
760 [DictionaryStringValues]
761 public interface IConversionsToString : IConversions
763 [TypeConverter(typeof(PhoneConverter))]
764 Phone Phone { get; set; }
767 public interface IStringLists
769 [DictionaryStringList]
770 IList<string> Names { get; }
772 [DictionaryStringList]
773 IList<int> Ages { get; set; }
776 public class PhoneConverter : TypeConverter
778 public override bool CanConvertFrom(ITypeDescriptorContext context,
779 Type sourceType)
781 if (sourceType == typeof(string))
783 return true;
785 return base.CanConvertFrom(context, sourceType);
788 public override object ConvertFrom(ITypeDescriptorContext context,
789 CultureInfo culture, object value)
791 if (value is string)
793 string[] fields = ((string) value).Split(new char[] {','});
794 return new Phone(fields[0], fields[1]);
796 return base.ConvertFrom(context, culture, value);
799 public override object ConvertTo(ITypeDescriptorContext context,
800 CultureInfo culture, object value, Type destinationType)
802 if (destinationType == typeof(string))
804 return ((Phone) value).Number + "," + ((Phone) value).Extension;
806 return base.ConvertTo(context, culture, value, destinationType);