public class TheDarkLord { public string Name { get; set; } private string SoName { get; set; } public TheDarkLord(string name, string soName) { this.Name = name; this.SoName = soName; } public string ModifyBaseName(string updatedName) { this.Name = updatedName; return this.Name; } }
[ContractClass(typeof(ITheDarkLordContract))] public interface ITheDarkLord { string Name { get; set; } string ModifyBaseName(string updatedName); }
[ContractClassFor(typeof(ITheDarkLord))] public abstract class ITheDarkLordContract : ITheDarkLord { public string Name { get { Contract.Ensures(!string.IsNullOrEmpty(Contract.Result<string>()), "[ITheDarkLordContract] Name of Dark Lord is null or empty."); return default(string); } set { Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(value), "[ITheDarkLordContract] Name of Dark Lord is null or empty."); } } public string ModifyBaseName(string updatedName) { Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(updatedName), "[ITheDarkLordContract.ModifyBaseName] The new name of Dark Lord is null or empty."); Contract.Ensures(!string.IsNullOrEmpty(Contract.Result<string>()), "[ITheDarkLordContract.ModifyBaseName] The updated name of Dark Lord is null or empty."); return default(string); } }
public class TheDarkLord : ITheDarkLord { public string Name { get; set; } private string SoName { get; set; } [ContractInvariantMethod] private void DarkLordVerification() { Contract.Invariant(!string.IsNullOrEmpty(SoName)); } public TheDarkLord(string name, string soName) { Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(name)); Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(soName)); this.Name = name; this.SoName = soName; } public string ModifyBaseName(string updatedName) { this.Name = updatedName; return this.Name; } }
[ContractClass(typeof(ITheWhiteLordContract))] public interface ITheWhiteLord : ITheDarkLord { string UpdateAllNames(string name, string soName); } [ContractClassFor(typeof(ITheWhiteLord))] public abstract class ITheWhiteLordContract : ITheDarkLordContract, ITheWhiteLord { public string UpdateAllNames(string name, string soName) { return default(string); } }
[ContractClass(typeof(ITheWhiteLordContract))] public interface ITheWhiteLord : ITheDarkLord { string UpdateAllNames(string name, string soName); } [ContractClassFor(typeof(ITheWhiteLord))] public abstract class ITheWhiteLordContract : ITheWhiteLord { public string Name { get; set; } public string ModifyBaseName(string updatedName) { return default(string); } public string UpdateAllNames(string name, string soName) { Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(name), "[ITheWhiteLordContract.UpdateAllNames] The new name of White Lord is null or empty."); Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(name), "[ITheWhiteLordContract.UpdateAllNames] The new soname of White Lord is null or empty."); Contract.Ensures(!string.IsNullOrEmpty(Contract.Result<string>()), "[ITheWhiteLordContract.UpdateAllNames] The updated name of White Lord is null or empty."); return default(string); } }
[ContractClass(typeof(ITheWhiteLordContract))] public interface ITheWhiteLord { string UpdateAllNames(string name, string soName); } [ContractClassFor(typeof(ITheWhiteLord))] public abstract class ITheWhiteLordContract : ITheWhiteLord { public string UpdateAllNames(string name, string soName) { Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(name), "[ITheWhiteLordContract.UpdateAllNames] The new name of White Lord is null or empty."); Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(name), "[ITheWhiteLordContract.UpdateAllNames] The new soname of White Lord is null or empty."); Contract.Ensures(!string.IsNullOrEmpty(Contract.Result<string>()), "[ITheWhiteLordContract.UpdateAllNames] The updated name of White Lord is null or empty."); return default(string); } }
public class TheWhiteLord : TheDarkLord, ITheWhiteLord { public string UpdateAllNames(string name, string soName) { return this.Name; } }
public class SpaceSheep { public ITheDarkLord AbstractLord { get; set; } }
public class SpaceSheep { public TheDarkLord AbstractLord { get; set; } }
public class TheDarkLord : ITheDarkLord { public string Name { get; set; } private string SoName { get; set; } [ContractInvariantMethod] private void DarkLordVerification() { Contract.Invariant(!string.IsNullOrEmpty(SoName)); } ... }
TheDarkLord lord = new TheDarkLord("James", "Bond");
CodeContracts: invariant is false: !string.IsNullOrEmpty(SoName)
TheDarkLord secondLord = new TheDarkLord() { Name = "James" };
Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(name), "[ITheWhiteLordContract.UpdateAllNames] The new name of White Lord is null or empty.");
Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(name), string.Format("{0} The new name of White Lord is null or empty.",GetCurrentMethod()));
Source: https://habr.com/ru/post/242945/
All Articles