public void CanExplicityConvertBetweenTypesWhenSupported() { TargetObject target = new TargetObject("Crowhurst"); //This works because of the explicit conversion operator declared by TargetObject that takes a TargetObject //and returns a SourceObject SourceObject source = (SourceObject)target; Assert.That(source.Surname, Is.EqualTo("Crowhurst")); }
public void CanImplicitlyConvertBetweenTypesWhenSupported() { SourceObject source = new SourceObject(); source.Surname = "Crowhurst"; //This works because of the implicit conversion operator declared by TargetObject that takes a SourceObject //and returns a TargetObject //Be aware that the operator could equally have been declared by SourceObject but not by both. //Also be aware that conversion operators are directional. For example the following would not compile //SourceObject someOtherSource = new TargetObject("Crowhurst"); //This is because there is no implcit operator that takes a TargetObject and return a SourceObject TargetObject target = source; Assert.That(target.FamilyName, Is.EqualTo("Crowhurst")); }