开发者

.NET Compact Framework Reflection call to internal method in .NET framework class

开发者 https://www.devze.com 2023-04-06 21:11 出处:网络
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 lik开发者_如何学编程e so:

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 lik开发者_如何学编程e 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?


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:

public string GenerateSalt()
{
    byte[] data = new byte[0x10];
    new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(data);
    return System.Convert.ToBase64String(data);
}

No need to use the SqlMembershipProvider (or reflection) at all.


Try using new Type[0] rather than new Type[]

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号