With the new dynamic capabilities in .NET 4.0, it seems like it should be possible to dynamically implement an interface, e.g. given:
public interface IFoo { string Bar(int baz); } public class Foo : IFoo { public string Bar(int baz) { return baz.ToString(); } } public class Proxy : IDynamicMetaObjectProvider { private readonly object target; public Proxy(object target) { this.target = target; } // something clever goes here }
Then I'm hoping there is some way to make it possible to write:
dynamic proxy = new Proxy(new Foo()); IFoo fooProxy = (IFoo)proxy; // because the target object implements it string bar = fooProxy.Bar(123); // delegates through to the target implementation
But, as yet, I'm unsure what to replace // something clever goes here
with.
So, my questions are:
Is this actually possible to do with the dynamic runtime? It appears that dynamically implementing things like methods and properties is fairly easy, but I haven't found any documentation about dynamically implementing interfaces and conversions to them.
Assuming this is possible, how difficult is it likely to be? (You can assume I'm a decent programmer with plenty of experience of things like reflection, but new to the dynamic framework.)
Are there any resources which would help to point me in the right direction for implementing something like this? Or even samples where this kind of thing has already been done that I can use as a starting point?
With C# 8.0, you can now have default implementations of methods in an interface. Interface members can be private, protected, and static as well. Protected members of an interface cannot be accessed in the class that extends the interface. Rather, they can be accessed only in the derived interface.
On implementation of an interface, you must override all of its methods. Interfaces can contain properties and methods, but not fields/variables. Interface members are by default abstract and public. An interface cannot contain a constructor (as it cannot be used to create objects)
You can create custom dynamic objects by using the classes in the System. Dynamic namespace. For example, you can create an ExpandoObject and specify the members of that object at run time. You can also create your own type that inherits the DynamicObject class.
Yes, you can write an interface without any methods. These are known as marking interfaces or, tagging interfaces. A marker interface i.e. it does not contain any methods or fields by implementing these interfaces a class will exhibit a special behavior with respect to the interface implemented.
The opensource framework Impromptu-Interface was designed to do this. It generates a cached lightweight proxy with a static interface and uses the dlr to forward the invocation to the original object.
using ImpromptuInterface; public interface ISimpeleClassProps { string Prop1 { get; } long Prop2 { get; } Guid Prop3 { get; } }
-
dynamic tOriginal= new ExpandoObject(); tOriginal.Prop1 = "Test"; tOriginal.Prop2 = 42L; tOriginal.Prop3 = Guid.NewGuid(); ISimpeleClassProps tActsLike = Impromptu.ActLike(tOriginal);
As far as I know, it's not possible without manual intervention to write or generate code that forwards the interface members to the wrapped instance. If you would like to see Microsoft-provided support for this sort of thing, you might want to consider voting at https://connect.microsoft.com/VisualStudio/feedback/details/526307/add-automatic-generation-of-interface-implementation-via-implementing-member .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With