实现在运行时的界面 [英] Implementing an Interface at Runtime

查看:184
本文介绍了实现在运行时的界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有可能使本已编译的类实现某个接口在运行时,一个例子:

Is it possible to make an already compiled class implement a certain interface at run time, an example:

public interface ISomeInterface {
    void SomeMethod();
}

public class MyClass {
    // this is the class which i want to implement ISomeInterface at runtime
}

这是可能的,如果是的话怎么办?

is this possible, if yes then how ?

推荐答案

好了差不多。您可以使用即兴接口。

Well almost. You can use impromptu-interface.

<一个href="https://github.com/ekonbenefits/impromptu-interface">https://github.com/ekonbenefits/impromptu-interface

从<一个基本的例子href="https://github.com/ekonbenefits/impromptu-interface/wiki/UsageBasic">https://github.com/ekonbenefits/impromptu-interface/wiki/UsageBasic

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>();

这篇关于实现在运行时的界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆