ekonbenefits / impromptu-interface

Static interface to dynamic implementation (duck casting). Uses the DLR combined with Reflect.Emit.
Apache License 2.0
656 stars 67 forks source link

FeatureRequest, ActLike with possibility to map method names #26

Open sjoerd222888 opened 6 years ago

sjoerd222888 commented 6 years ago

Hi

I think it is not yet possible what I would like to do. Let's assume I have

public class MyAdder{
    public double Add(double a, double b){
        return a + b;
    }
}

and an interface

public interface ISumCalculater{
    double Sum(double value1, double value2);
}

What I would like to be able to do is:

MyAdder adder = new MyAdder();
ISumCalculater calculator = adder.ActLike<ISumCalculater>().MapMethod("Add", "Sum");
double result = calculator.Sum(1.0, 2.0);

So I would like to have the possibility to have different method names in the interface and the implementation and still have the possibility to use ActLike<>. So I could use implementations which are compatible on the 'logical level' (however that should be called), but not on the naming level (which from a functional point of view is secondary; whether I call a car a "car", "voiture" or "автомобиль" does not change its behavior and functionality).

Now my question is whether this (by some way that I overlooked) is already possible in your library. If not then I would be interested to know whether this library could be extended to support that scenario (not only technically but also whether it would fit "the intention" of the library in your opinion).

Potentially this also interesting of course for properties.

jbtule commented 6 years ago

While there isn't a dynamic map feature already in impromptu interface (there is a static map feature, https://github.com/ekonbenefits/impromptu-interface/blob/master/ImpromptuInterface/src/Attributes/AliasAttribute.cs).

Implementing a dynamic mapper on your own wouldn't be that hard using Dynamitey.DynamicObjects.BaseForwarder https://github.com/ekonbenefits/dynamitey/blob/master/Dynamitey/DynamicObjects/BaseForwarder.cs