Is there a way to create a DynamicObject that supports an Interface?

后端 未结 3 2227
逝去的感伤
逝去的感伤 2021-02-19 15:27

Can I define a class which derives from DynamicObject and supports an interface (ICanDoManyThings) without having to implement each method in the interface?

I\'m trying

3条回答
  •  佛祖请我去吃肉
    2021-02-19 15:44

    With Clay, you can.

    An example:

    public interface IMyInterface
    {
        string Prop1 { get; }
    
        long Prop2 { get; }
    
        Guid Prop3 { get; }
    
        Func Meth { get; }
    }
    
    //usage:
    
    dynamic factory = new ClayFactory();
    var iDynamic = factory.MyInterface
    (
        Prop1: "Test",
        Prop2: 42L,
        Prop3: Guid.NewGuid(),
        Meth: new Func(i => i > 5)
    );
    
    IMyInterface iStatic = iDynamic;
    

    This article shows few more ways to achieve this.

提交回复
热议问题