So I am loading an assembly at runtime and I also am using generics depending on the assembly type. To get the right type, each DLL is implementing a factory that I expect to be there which开发者_如何学Python instantiates the class with the correct generic type.
Type factoryType = assembly.GetType("MyCompany.ScenarioPlayer.PlayerFactory");
MethodInfo method = factoryType.GetMethod("CreatePlayer", BindingFlags.Public | BindingFlags.Static);
player = (IScenarioPlayer)method.Invoke(null, null);
This code fails on the method.Invoke when I don't have administrative privileges. Is this correct that any calls to MethodInfo.Invoke the way I'm using it requires admin? I stepped down into my DLL's code and it isn't even getting past this call into the Factory.
edit : turns out there was a dependency for the target DLL that was not accounted for. I do like the MEF framework idea proposed though.
Using reflection, in the way you're using it, to solve the problem of creating factories at runtime is a bad idea. Not only are you not avoid coupling (you've got a hard-coded string referencing a single type), you're opening up a world of hurt. None of this stuff is strongly typed.
I would suggest looking at something like MEF if you're looking to create pluggable components. It already solves the problems you're facing and is does it in a much nicer, faster and more maintainable way.
This doesn't have anything to do with MethodInfo, but rather what MethodInfo is doing most likely. You can test this out by calling the method in question and seeing if it works in a non admin environment.
精彩评论