Reputation: 18744
I'm struggling to fake these two lines of code:
Assembly asm = Assembly.LoadFile(fileName);
Type[] asmTypes = loggerAssembly.GetTypes();
When I type System.Reflection.ShimAssembly
there is no such type as ShimAssembly
like for example in the case of System.IO.ShimFile
but only a StubAssembly
.
I could refactor the code and do it with a static helper method like LoadAssemblyAndGetTypes
but it seems to be an unnecessary workaround. I'd prefer the official solution.
If it only worked like this:
var shimAsm = System.Reflection.Fakes.ShimAssembly.LoadFile = (fileName) =>
{
return ???
};
var types = shimAsm.GetTypes = () =>
{
return new Type[] { new object() };
};
Why does it work for System.IO.File
and not for System.Reflection.Assembly
. Is this because Assembly
is an abstract class?
In my unit test I want to check if my assembly loader correctly checks if the loaded assembly contains types that implement some interfaces so that I can instantiate them later later.
Upvotes: 4
Views: 901
Reputation: 18744
I think I found the answer and it doesn't look good:
@Patrick Tseng - Visual Studio team - writes at Shim mscorlib and system limitations:
...we DO have a list of type we purposely not allowing them to be shimmed. Reason being that it could potentially cause recurisvely calling into your detour delegate from CLR runtime itself. E.g If CLR runtime uses a type in System.Reflection during runtime and you happen to detour functions in this type. You might end up causing expected behavior since runtime behavior will totally be changed.
In short, we don't shim value type, System.Reflection., System.Runtime., XamlGeneratedNamespace, and a few other types which we deem is important and will not shim them.
so after all it seems I'll need to keep the static helper method.
Upvotes: 4