0

I am dealing with a 3rd-party-library sealed internal type. Let's call this type VendorInternalTypeA. It is used in the assembly VendorLibraryA.

I want to substitute a different type, one of my own, for VendorInternalTypeA in the a couple of places. This works better than you might expect, but there is a sticking-place, also in 3rd-party internal code:

// Example code to illustrate the problem: no actual ArrayLists are being used.

void SaveA(object maybeA) {
   arrayList.Add(maybeA);
}

VendorInternalTypeA LoadA() {
   return (VendorInternalTypeA) arrayList[0];
}

What this means is that LoadA is expecting an instance of VendorInternalTypeA, or at least something that it can explicitly cast to VendorInternalTypeA. A subclass would work well here -- but VendorInternalTypeA is sealed, so I can't subclass it.

It occurred to me, after a bit of pondering, that if I could define a class that implemented an explicit operator for VendorInternalTypeA, that cast would work, and I'd be able to pass in instances of my own class and have them play nicely!

I got all excited, until I remembered that VendorInternalTypeA is also an internal class, so I can't reference it directly in my code.

So, my question to you: is there a way around this, with reflection, IL emission, or other slightly hacky approaches? A way to reference this internal class in the definition of another class?

(I am 90% to giving up on the project and saying it can't be done at all -- and still may, even if the answer is "yes, and here's how". But this is a really interesting question to me.)

Ann L.
  • 13,760
  • 5
  • 35
  • 66
  • Decompile the source for VendorInternalTypeA, and paste it into your own application? – Robert Harvey Sep 30 '21 at 15:07
  • How do you are doing the "substitute" part right now? – Ygalbel Sep 30 '21 at 15:08
  • @RobertHarvey It still would be a different type! And .NET doesn't allow "duck typing" (unless something has changed recently, and even so it'd probably only be under certain circumstances!) – Ann L. Sep 30 '21 at 15:09
  • This is almost certainly an [X Y problem.](https://xyproblem.info/) – Robert Harvey Sep 30 '21 at 15:11
  • @Ygalbel Most places in the third-party code where this object is stored and retrieved are in virtual methods of public classes. So I subclassed them and implemented a mapping between `VendorInternalTypeA` and a custom class, and used the custom class in my overridden methods. – Ann L. Sep 30 '21 at 15:13
  • But, there's one place where I absolutely can't do that, and that's in the example code above. – Ann L. Sep 30 '21 at 15:14
  • @RobertHarvey You are probably right. I'm close to abandoning the project. But if I could have got it to work (in a non-smelly way), it would not only have been cool and elegant, it would have saved me weeks of tedious coding to replicate what the vendor code does. – Ann L. Sep 30 '21 at 15:17
  • @RobertHarvey At this point, I mostly just want to know if it's even a thing one can do. – Ann L. Sep 30 '21 at 15:17
  • 2
    I sort of hope not. The whole point of marking things internal and private is to prevent these sort of things from happening. – Robert Harvey Sep 30 '21 at 15:18
  • 1
    @RobertHarvey Yeah, any such solution is going to be code-smelly and brittle as heck. I concede that point. I'm operating out of curiosity at this point. – Ann L. Sep 30 '21 at 15:19
  • Would it be possible to use GetType, passing in the full qualified string for the internal class name, and use Expression.Convert to achieve the second line of your example? – Lee Sep 30 '21 at 15:47
  • @Lee No, given that they've already stated it's code in a 3rd party library that can't be changed. If they could change it they wouldn't need to do anything so complicated, they could just cast the object to the actual type it is and be done with it. – Servy Sep 30 '21 at 15:54
  • But wouldn’t getType still allow you to get access to the internal type through reflection? Yea I understand this isn’t best practices, just wondering if it’s possible. For example: https://stackoverflow.com/a/27334057/9491881 – Lee Sep 30 '21 at 15:57

1 Answers1

4

No, there is not. Even if the class was public and you did have an explicit conversion to VendorInternalTypeA from your class, the code in question is casting an expression of type object to VendorInternalTypeA not an expression of your custom type, so the explicit conversion operator won't be used even if it exists.

User defined conversion operators are bound at compile time when the compile time type of the expression being converted has a user defined operator to the type it's being converted to.

Servy
  • 202,030
  • 26
  • 332
  • 449