1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
19 using System
.Collections
;
20 using System
.Collections
.Generic
;
21 using System
.ComponentModel
;
22 using System
.Globalization
;
23 using NUnit
.Framework
;
26 public class DictionaryAdapterFactoryTestCase
28 private IDictionary dictionary
;
29 private DictionaryAdapterFactory factory
;
34 dictionary
= new Hashtable();
35 factory
= new DictionaryAdapterFactory();
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
);
52 public void CreateAdapter_PrefixPropertiesOnly_WorksFine()
54 IPerson person
= factory
.GetAdapter
<IPersonWithPrefix
>(dictionary
);
55 Assert
.IsNotNull(person
);
59 public void UpdateAdapter_NoPrefix_UpdatesDictionary()
61 IPerson person
= factory
.GetAdapter
<IPerson
>(dictionary
);
62 person
.Name
= "Craig";
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
);
74 public void UpdateAdapter_Prefix_UpdatesDictionary()
76 IPerson person
= factory
.GetAdapter
<IPersonWithPrefix
>(dictionary
);
77 person
.Name
= "Craig";
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
);
88 public void UpdateAdapterAndRead_NoPrefix_Matches()
90 IPerson person
= factory
.GetAdapter
<IPerson
>(dictionary
);
91 person
.Name
= "Craig";
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
);
103 public void UpdateAdapterAndRead_Prefix_Matches()
105 IPerson person
= factory
.GetAdapter
<IPersonWithPrefix
>(dictionary
);
106 person
.Name
= "Craig";
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
);
118 public void UpdateAdapterAndRead_PrefixOverride_Matches()
120 IPerson person
= factory
.GetAdapter
<IPersonWithPrefixOverride
>(dictionary
);
121 person
.Name
= "Craig";
123 Assert
.AreEqual("Craig", dictionary
["Name"]);
127 public void UpdateAdapterAndRead_TypePrefix_Matches()
129 IPersonWithTypePrefixOverride person
=
130 factory
.GetAdapter
<IPersonWithTypePrefixOverride
>(dictionary
);
133 Assert
.AreEqual(72, person
.Height
);
134 Assert
.AreEqual(72, dictionary
["Castle.Components.DictionaryAdapter.Tests.IPersonWithTypePrefixOverride#Height"]);
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
);
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
);
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;
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
);
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"]);
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"]);
213 public void CreateAdapter_WithComponent_WorksFine()
215 IPerson person
= factory
.GetAdapter
<IPerson
>(dictionary
);
216 IAddress mailing
= person
.HomeAddress
;
217 Assert
.IsNotNull(mailing
);
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
);
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";
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
);
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
);
271 public void UpdateAdapter_WithComponentOverrideNoPrefix_WorksFine()
273 IPerson person
= factory
.GetAdapter
<IPerson
>(dictionary
);
274 IAddress work
= person
.WorkAddress
;
275 work
.Line1
= "139 Dartbrook";
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
);
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
);
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";
320 public void CreateAdapter_WithNestedComponent_WorksFine()
322 IPerson person
= factory
.GetAdapter
<IPerson
>(dictionary
);
323 IAddress mailing
= person
.HomeAddress
;
324 Assert
.IsNotNull(mailing
.Phone
);
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
);
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
);
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
);
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
);
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"]);
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
);
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"]);
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
);
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"]);
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
);
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"]);
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]);
538 public void UpdateAdapter_WithStringLists_WorksFine()
540 IStringLists lists
= factory
.GetAdapter
<IStringLists
>(dictionary
);
542 IList
<string> names
= lists
.Names
;
545 names
.Add("Kaitlyn");
547 names
.Add("Matthew");
549 Assert
.AreEqual("Craig,Brenda,Kaitlyn,Lauren,Matthew", dictionary
["Names"]);
551 IList
<int> ages
= new List
<int>();
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
;
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; }
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
;
620 private string state
;
621 private string zipCode
;
622 private IPhone phone
;
623 private IPhone mobile
;
624 private IPhone emergency
;
628 get { return line1; }
629 set { line1 = value; }
634 get { return line2; }
635 set { line2 = value; }
641 set { city = value; }
646 get { return state; }
647 set { state = value; }
650 public string ZipCode
652 get { return zipCode; }
653 set { zipCode = value; }
674 mobile
= new Phone();
681 public IPhone Emergency
685 if (emergency
== null)
687 emergency
= new Phone();
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
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
,
798 if (sourceType
== typeof(string))
802 return base.CanConvertFrom(context
, sourceType
);
805 public override object ConvertFrom(ITypeDescriptorContext context
,
806 CultureInfo culture
, object value)
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
);