MethodInfo.Invoke 在 Windows 7 上需要管理员权限吗?
因此,我在运行时加载程序集,并且还根据程序集类型使用泛型。为了获得正确的类型,每个 DLL 都实现了一个工厂,我希望该工厂能够使用正确的泛型类型实例化该类。
Type factoryType = assembly.GetType("MyCompany.ScenarioPlayer.PlayerFactory");
MethodInfo method = factoryType.GetMethod("CreatePlayer", BindingFlags.Public | BindingFlags.Static);
player = (IScenarioPlayer)method.Invoke(null, null);
此代码在方法上失败。当我没有管理权限时调用。我使用它的方式对 MethodInfo.Invoke 的任何调用都需要管理员,这是正确的吗?我进入了 DLL 的代码,但它甚至没有通过对 Factory 的调用。
编辑:结果发现目标 DLL 存在未考虑的依赖项。不过,我确实喜欢提出的 MEF 框架想法。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按照您使用反射的方式来解决在运行时创建工厂的问题是一个坏主意。您不仅没有避免耦合(您有一个引用单一类型的硬编码字符串),而且您正在打开一个充满伤害的世界。这些东西都不是强类型的。
如果您想创建可插入组件,我建议您查看类似 MEF 的内容。它已经解决了您所面临的问题,并且以更好、更快、更易于维护的方式实现。
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.
这与 MethodInfo 没有任何关系,而与 MethodInfo 最有可能正在做的事情有关。您可以通过调用相关方法并查看它是否在非管理环境中工作来测试这一点。
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.