首页 .Net .Net(C#) 使用ImpromptuInterface动态实现的静态接口(duck casting)

.Net(C#) 使用ImpromptuInterface动态实现的静态接口(duck casting)

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


特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。