Invoke "internal extern" constructor using reflections

Posted by Riz on Stack Overflow See other posts from Stack Overflow or by Riz
Published on 2010-04-16T16:44:40Z Indexed on 2010/04/16 16:53 UTC
Read the original article Hit count: 389

Filed under:
|
|

Hi, I have following class (as seen through reflector)

public class W : IDisposable
{
    public W(string s);
    public W(string s, byte[] data);

    // more constructors

    [MethodImpl(MethodImplOptions.InternalCall)]
    internal extern W(string s, int i);

    public static W Func(string s, int i);

}

I am trying to call "internal extern" constructor or Func using reflections

MethodInfo dynMethod = typeof(W).GetMethod("Func", BindingFlags.Static);                
object[] argVals = new object[] { "hi", 1 };
dynMethod.Invoke(null, argVals);

and

Type type = typeof(W);
Type[] argTypes = new Type[] { typeof(System.String), typeof(System.Int32) };
ConstructorInfo cInfo = type.GetConstructor(BindingFlags.InvokeMethod | BindingFlags.NonPublic, null, argTypes, null);
object[] argVals = new object[] { "hi", 1 };
dynMethod.Invoke(null, argVals);

unfortunantly both variants rise NullReferenceException when trying to Invoke, so, I must be doing something wrong?

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection