0

I have a test method where I call an private function that converts a kind of to another kind.

This static function have the following signature:

private static Destiny[] Array2Array<Origin,Destiny> (Origin[] OriginVector)

Since it's a private function, the tester give an error saying it cannot access it. So I got to this point:

Origin[] OriginVector = null; // TODO: Initialize to an appropriate value
Destiny[] expected = null; // TODO: Initialize to an appropriate value
Destiny[] actual;
var dummy = new ConversionClass();
var po = new PrivateObject( dummy, new PrivateType(typeof(ConversionClass)));
var acessor = new ConversionClassAcessor(po);

actual = po.Invoke("Array2Array", 
         new [] { typeof(Origin[]), typeof(Destiny[]) }, 
         new object[] { OriginVector } );

EDIT: That last line throws an compiler error with the message "cannot convert type object to Destiny[]". What I'm doing wrong?

Fabricio Araujo
  • 3,810
  • 3
  • 28
  • 43
  • What happened here? Mr Shain? – Fabricio Araujo Dec 22 '11 at 21:10
  • Instead of using the `Invoke` method, you could consider using the assembly attribute [`InternalsVisibleTo`](http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx). It'll make your tests much cleaner. – Matthias Dec 22 '11 at 21:16
  • @MatthiasKoch: the one that solved the question **deleted** his anwer(?!?!?!?) before I could mark it as accepted. But the attribute is nice. – Fabricio Araujo Dec 22 '11 at 21:58
  • what solved it? I didn't notice other answers.. – Matthias Dec 22 '11 at 22:02
  • He deleted his answer just a few seconds after posting - I was posting a comment when SO warned that the answer was deleted!!! – Fabricio Araujo Dec 22 '11 at 22:05
  • The call was solved but it doesn't see the private method, in the end I created an public function with an `Obsolete` attribute just to test it. – Fabricio Araujo Dec 23 '11 at 18:09

2 Answers2

0

Mr Chris Shain,

I will here reproduce the solution you gave me. Since you deleted your answer, if you add a new one after this, I'll delete this and accept yours as the question's answer.


The problem with the code above is that actual variable is of type Destiny[] and the result of Invoke is System.Object. An typecast is needed:

actual = (Destiny[]) po.Invoke("Array2Array", 
         new [] { typeof(Origin[]), typeof(Destiny[]) }, 
         new object[] { OriginVector } );
Fabricio Araujo
  • 3,810
  • 3
  • 28
  • 43
  • Ohhh... I thought you didn't get this error?! Because you said "it does NOT compile WITH an error".. :/ – Matthias Dec 22 '11 at 22:07
  • It could be better phrased, I agree: "it does not compile, with a "blabla" error message". Anyway I edited it and expect this time to be clearer ;-) – Fabricio Araujo Dec 22 '11 at 22:15
0

The answer is simple.. cast it. :D

actual = (Destiny[]) po.Invoke("Array2Array", 
     new [] { typeof(Origin[]), typeof(Destiny[]) }, 
     new object[] { OriginVector } );
Matthias
  • 15,919
  • 5
  • 39
  • 84