Dynamically implementing an interface in .NET 4.0 (C#)

The opensource framework Impromptu-Interface was designed to do this. It generates a cached lightweight
proxy with a static interface and uses the dlr to forward the invocation to the original object.

using ImpromptuInterface;

public interface ISimpeleClassProps
{
    string Prop1 { get;  }

    long Prop2 { get; }

    Guid Prop3 { get; }
}

dynamic tOriginal= new ExpandoObject();
tOriginal.Prop1 = "Test";
tOriginal.Prop2 = 42L;
tOriginal.Prop3 = Guid.NewGuid();

ISimpeleClassProps tActsLike = Impromptu.ActLike(tOriginal);

Leave a Comment