.NET Compact Framework 对 .NET 框架类中的内部方法的反射调用
我正在尝试从 .NET 2.0 CF 应用程序加载 .NET 程序集。我想调用具有三个参数的内部方法,如下所示:
var obj = new System.Web.Security.SqlMembershipProvider();
MethodInfo mi = obj.GetType().GetMethod("GenerateSalt",
BindingFlags.NonPublic | BindingFlags.Instance,
null, new Type[] {}, null);
object resObj = mi.Invoke(obj, new object[] {});
当执行 GetMethod 调用时,会引发 InvalidProgramException。我可以从常规 .NET 2.0 控制台应用程序测试工具进行此调用,但在 .NET 2.0 CF 中它会抛出异常。
到底是怎么回事?
I am trying to load a .NET assembly from a .NET 2.0 CF application. I want to invoke an internal method with three params like so:
var obj = new System.Web.Security.SqlMembershipProvider();
MethodInfo mi = obj.GetType().GetMethod("GenerateSalt",
BindingFlags.NonPublic | BindingFlags.Instance,
null, new Type[] {}, null);
object resObj = mi.Invoke(obj, new object[] {});
When the GetMethod call is executed, a InvalidProgramException is thrown. I can make this call from a regular .NET 2.0 console app test harness, but in .NET 2.0 CF it throws.
What is going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法从 Compact Framework 项目引用包含
SqlMembershipProvider
的程序集 (System.Web.dll)。据我所知,这种类型在 Compact Framework 中不可用。您可能会收到异常,因为您正在加载的程序集包含 Compact Framework 运行时无法理解的 IL。
然而,自己重写GenerateSalt所做的事情相当简单,并且紧凑的框架应该具备使其工作所需的一切:
根本不需要使用SqlMembershipProvider(或反射)。
You cannot reference the assembly (System.Web.dll) that contains
SqlMembershipProvider
from a Compact Framework project. As far as I can tell, this type is not available in the Compact Framework.You are likely getting the exception because you are loading an assembly that contains IL that the Compact Framework Runtime cannot understand.
However, it is fairly simple to re-write what GenerateSalt does yourself, and the compact framework should have everything needed to make it work:
No need to use the SqlMembershipProvider (or reflection) at all.
尝试使用 new Type[0] 而不是 new Type[]
Try using new Type[0] rather than new Type[]