Implementing an Interface at Runtime

后端 未结 2 456
野的像风
野的像风 2021-01-15 17:21

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

public interface ISomeInterface {
    void SomeMethod         


        
2条回答
  •  梦毁少年i
    2021-01-15 17:54

    Well almost. You can use impromptu-interface.

    https://github.com/ekonbenefits/impromptu-interface

    Basic example from 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();
    

提交回复
热议问题