1、ImpromptuInterface的安装引用
通过Nuget引用,使用Nuget图形管理器=》搜索"ImpromptuInterface"=》找到然后点击"安装"。
相关文档:VS(Visual Studio)中Nuget的使用
2、使用示例代码
using ImpromptuInterface;
using Dynamitey;
public interface IMyInterface{
string Prop1 { get; }
long Prop2 { get; }
Guid Prop3 { get; }
bool Meth1(int x);
}
//Anonymous Class
var anon = new {
Prop1 = "Test",
Prop2 = 42L,
Prop3 = Guid.NewGuid(),
Meth1 = Return<bool>.Arguments<int>(it => it > 5)
}
var myInterface = anon.ActLike<IMyInterface>();
或
//Dynamic Expando object
dynamic expando = new ExpandoObject();
expando.Prop1 ="Test";
expando.Prop2 = 42L;
expando.Prop3 = Guid.NewGuid();
expando.Meth1 = Return<bool>.Arguments<int>(it => it > 5);
IMyInterface myInterface = Impromptu.ActLike(expando);
官方地址:https://github.com/ekonbenefits/impromptu-interface