UsageBasic

jbtule edited this page May 2, 2013 · 1 revision

Introduction

The primary feature of ImpromptuInterface allows you to wrap any object, POCO or Dynamic with an Interface that informally matches up with the implementation even though it was not formally declared. And really it works with any. There are a lot of "duck casting" frameworks out there than only work with specific kinds of objects, but with ImpromptuInterface there are no special cases and it uses the DLR directly so it even works with objects that implement IDynamicMetaObjectProvider.

Examples

    using ImpromptuInterface;

    public interface ISimpleClassProps
    {
        string Prop1 { get;  }

        long Prop2 { get; }

        Guid Prop3 { get; }
    }

...

    var tAnon = new {Prop1 = "Test", Prop2 = 42L, Prop3 = Guid.NewGuid()};

    var tActsLike = tAnon.ActLike<ISimpleClassProps>();

Or


    using ImpromptuInterface.Dynamic;

    public interface ISimpleWithMeth : ISimpleClassProps{
        bool Meth1(int x);
   }

...

    dynamic tNew = new ExpandoObject();
    tNew.Prop1 = "Test";
    tNew.Prop2 = 42L;
    tNew.Prop3 = Guid.NewGuid();
    tNew.Meth1 = Return<bool>.Arguments<int>(it => it > 5);

    ISimpleWithMeth tActsLike = Impromptu.ActLike(tNew);