0

I have PCL class Image and a method inside the class to load the image:

 public Image(string fileName) //constructor
   {
     //platform specific implementation
   }

Can I have write another assembly to override this method (hide this method by a method in another assembly - like TypeForwarding).

In general I want that user selects: Image assembly and some platform specific one e.g. Image.Windows

Is there any ways I can do that ? (some workarounds ?)

Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
dajuric
  • 2,373
  • 2
  • 20
  • 43

2 Answers2

2

[TypeForwardTo] is used to hide stuff, declarations that are not available on the target platform once the program executes and starts using the real assemblies. You don't want to hide, you want to replace.

Which certainly is possible, you just need a collection of assemblies whose declarations are identical to the reference assembly but have methods that work differently, adapted to the specific platform they run on.

You do have one problem, making sure that the reference assembly is absolutely identical to the implementation assembly. You need an exact match for the fully qualified assembly name, that's doable, and an exact match for every type and method signature. That's not so doable. The kind of tooling that Microsoft used to generate the reference assemblies in PCL are not available to us. You can hoof it, but one small mistake and you're pretty screwed. Realistically, you have to create your own tooling to double-check for an exact match. Maintenance is also quite painful.

Just don't, there are already excellent patterns in .NET to give you this. Use interfaces.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

I did some similar research.

I found very little information on that, and honestly, it is really complicated to implement in .Net.

But I'll suggest you to look here

How do I intercept a method call in C#?

For the original question :

There is no way you can override this method for other classes to use your own.

My suggestion would be to make your "select source" interface before you initialize Image. So you can choose a method of Initialization

Community
  • 1
  • 1
Jurion
  • 1,230
  • 11
  • 18