示例#1
0
    static void Main(string[] args)
    {
        dynamic person = new { Name = "Peter" };
        var     p      = DuckType.As <IPerson>(person);

        Console.WriteLine(p.Name);
    }
示例#2
0
 [Ignore] //Manual test
 public void As_Loop()
 {
     foreach (object obj in GetObjects())
     {
         Assert.IsNotNull(DuckType.As <IHasValue>(obj).Value);
     }
 }
示例#3
0
        public void As_Invalid()
        {
            string        expected = Guid.NewGuid().ToString();
            BespokeString bespoke  = new BespokeString(expected);

            Assert.IsNotInstanceOfType(bespoke, typeof(IHasValue));
            IHasValue duck = DuckType.As <IHasValue>(bespoke);

            Assert.IsNull(duck);
        }
示例#4
0
        public void As_Struct()
        {
            int        expected = _random.Next();
            BespokeInt bespoke  = new BespokeInt(expected);

            Assert.IsNotInstanceOfType(bespoke, typeof(IHasValue));
            IHasValue duck = DuckType.As <IHasValue>(bespoke);

            Assert.IsInstanceOfType(duck, typeof(IHasValue));
            Assert.AreEqual(expected, duck.Value);
        }
示例#5
0
        public void As()
        {
            DateTime        expected = new DateTime(_random.Next(1900, 2000), _random.Next(1, 12), _random.Next(1, 28));
            BespokeDateTime bespoke  = new BespokeDateTime(expected);

            Assert.IsNotInstanceOfType(bespoke, typeof(IHasValue));
            IHasValue duck = DuckType.As <IHasValue>(bespoke);

            Assert.IsInstanceOfType(duck, typeof(IHasValue));
            Assert.AreEqual(expected, duck.Value);
        }
示例#6
0
 /// <summary>
 /// Returns the object, duck-typed to the given interface
 /// </summary>
 /// <typeparam name="TInterface"></typeparam>
 /// <param name="obj"></param>
 /// <returns></returns>
 public static TInterface As <TInterface>(this object obj)
 {
     return(DuckType.As <TInterface>(obj));
 }