1

I have the following usecase. There'a ISpecificInterface interface with SpecificProperty property that is explicitly implemented by SpecificInterfaceImplementation class. My code implements the callback that is passed object which refers to the SpecificInterfaceImplementation instance. The problem gets worse - there're several different versions of ISpecificInterface each having SpecificProperty with the same type (of the property) possible and my program needs to work with any of them and preferably without code duplication.

I'd use duck typing via C# dynamic:

dynamic theInterface = theObjectPassed;
String propertyValue = theInterface.SpecificProperty;

but since the property is explicitly implemented I get RuntimeBinderException with the following text

'SpecificNamespace.SpecificInterfaceImplementation' does not contain a definition for 'SpecificProperty'

and so I need to somehow get to the interface. I would not use a cast because the cast would expose a specific version of the interface and work only for that version and duck typing would be gone. So I use Reflection directly:

Type objectType = theObjectPassed.GetType();
var specificInterface = objectType.GetInterface("SpecificInterface");
var specificProperty = specificInterface.GetProperty("SpecificProperty");
var propertyValue = specificProperty.GetValue(specificInterface);

and it works just fine but it requires a ton of extra code.

Can I somehow use dynamics and duck typing to avoid this ton of code with Reflection?

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • Ton of code? 4 lines? Come on... – Alberto Feb 24 '14 at 11:07
  • @Alberto: Large difference compared to just `theInterface.SpecificProperty`. – sharptooth Feb 24 '14 at 11:18
  • Probably I'm doing something wrong, but it works perfectly on my machine as I would expect it. dynamic refers to _runtime_ type of instance it operates on, and if it can not find a field, that means that field actually does not exist in that type. – Tigran Feb 24 '14 at 11:36
  • @Tigran: Sure and if the *runtime type* implements the property explicitly the property will be inaccessible. – sharptooth Feb 24 '14 at 11:40
  • @sharptooth: look here: http://dotnetfiddle.net/HacuWe. Is this what you mean? – Tigran Feb 24 '14 at 11:50
  • Have an interface that all of the ISpecificInterface are based on with this property, then just cast to that? – Joe Feb 24 '14 at 12:03
  • 1
    @Tigran: Nope, something like this: http://dotnetfiddle.net/3OWv1e – sharptooth Feb 24 '14 at 12:04
  • @Joe: I cannot introduce a new interface - I'm just implementing teh callback. Whatever comes to that callback I should handle. – sharptooth Feb 24 '14 at 12:05
  • @sharptooth sounds like a poor design. I'd throw the reflection stuff you've already got into a method and use that. This isn't what `dynamic` was intended for. – Joe Feb 24 '14 at 12:08

1 Answers1

0

I didn't use so much the dynamic feature of C#, but, from what I know and I see in your code, you should be able to access to the property SpecificProperty of theInterface, since it's declared explicitly.

My two cents: Using Reflection, the same task is achieved and I don't see a greater saving of lines of code (from 1 line to just 4 lines).

EDIT: With a little research, I found that your code is fine (I think it also compiles perfectly, because the dynamic reference is resolved at runtime) and this could be a bug in the dynamic feature, as explained in this case.

Community
  • 1
  • 1
Alberto Solano
  • 7,972
  • 3
  • 38
  • 61