Mercurial > pub > bltoolkit
view HowTo/Patterns/DuckTyping.cs @ 5:f7d63a092920
Исправлено условие Where в тех случаях, когда репозитарий не является генериком
author | cin |
---|---|
date | Tue, 10 Mar 2015 16:02:11 +0300 |
parents | f990fcb411a9 |
children |
line wrap: on
line source
using System; using NUnit.Framework; using BLToolkit.Patterns; namespace HowTo.Patterns { //[TestFixture] public class DuckTyping { // By default, all interface methods are optional. // public interface OptionalInterface { // If the source object does not implement the [b]Method1[/b], a NotImplementedException is thrown at [i]run time[/i]. // void Method1(); // If the source object does not implement the [b]Method2[/b], a TypeBuilderException is thrown at [i]build time[/i]. // /*[a]*/[MustImplement]/*[/a]*/ void Method2(); // If the source object does not implement the [b]Method3[/b], an empty stub is genegated. // The return value and all output parameters will be set to default values. // /*[a]*/[MustImplement(false, false)]/*[/a]*/ int Method3(); } // The MustImplement attribute also can control the entire interface. // /*[a]*/[MustImplement]/*[/a]*/ public interface RequiredInterface { // If the source object does not implement the [b]Method1[/b], a TypeBuilderException is thrown at [i]build time[/i]. // void Method1(); // If the source object does not implement the [b]Method2[/b], a NotImplementedException is thrown at [i]run time[/i]. // /*[a]*/[MustImplement(false)]/*[/a]*/ void Method2(); // If the source object does not implement the [b]Method3[/b], an empty stub is genegated. // The return value and all output parameters will be set to default values. // /*[a]*/[MustImplement(false, false)]/*[/a]*/ int Method3(); } public class TestClass { public int Method3() { return 3; } } // Two or more interfaces can be mixed together. // public interface InterfaceMix : RequiredInterface, OptionalInterface { } //[Test] public void Test() { InterfaceMix duck = /*[a]*/BLToolkit.Patterns.DuckTyping.Implement/*[/a]*/<InterfaceMix>(new TestClass()); RequiredInterface duck1 = duck; OptionalInterface duck2 = duck; duck1.Method1(); duck2.Method1(); } } }