none
Error invoking constructor using reflection RRS feed

  • Question

  • I have 

    public class A{}
    public class B
        {
            A a;
            public B(A a)
            {
                this.a =a;
            }
    }

    Now i am trying to invoke constructor of B using reflection.

    A a = new A();
    var type = typeof(B);
    var ctr = type.GetConstructor(new []{typeof(A)});
    var instance = ctr.Invoke(new Object[] { a });
    Console.Out.Write(null != instance);

    This runs fine in my local system however failing in the build server(using NAnt) and giving me an exception

    System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
      ----> System.InvalidProgramException : Common Language Runtime detected an invalid program.
    at System.RuntimeMethodHandle._InvokeConstructor(Object[] args, SignatureStruct& signature, IntPtr declaringType)
    at System.RuntimeMethodHandle.InvokeConstructor(Object[] args, SignatureStruct signature, RuntimeTypeHandle declaringType)
    at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
    at Program..ctr(A a) in Program.cs:line 20
    --InvalidProgramException

    Any idea whats going wrong here and how can i debug further ?

    Saturday, August 17, 2013 6:35 PM

Answers

  • "I thought OS does not matter as long as we are using same .net platform. Isn't it the case?"

    Well, certainly yes. The only things I can think of are:

    • there was a bug in .NET and the working machine has a fix for it and the server does not (say, working machine has .NET 3.5 SP1 and the server has .NET 3.5 without SP)
    • there's a difference in the .NET security config of the server that prevents reflection invoke - I doubt that, I think the error message would be different in this case.

    Try building this code on the server using the C# compile directly and then run the exe, see what happens.

    I'll move this to the CLR forums as it looks like a CLR problem, not BCL. Perhaps someone from Microsoft sees this and has an idea.

    Sunday, August 18, 2013 8:06 AM
    Moderator

All replies

  • That's rather strange, I don't see anything wrong with that code. Is there any difference between the build server and your system? Different .NET version or different CPU architecture (x86/x64)?

    Saturday, August 17, 2013 7:18 PM
    Moderator
  • Yes, they are different. Local system is win 7 where as build server is win 2003. Both are x64 bit and running on .net 3.5. I thought OS does not matter as long as we are using same .net platform. Isn't it the case? How can I debug further to see what's going wrong here ?
    Sunday, August 18, 2013 6:20 AM
  • "I thought OS does not matter as long as we are using same .net platform. Isn't it the case?"

    Well, certainly yes. The only things I can think of are:

    • there was a bug in .NET and the working machine has a fix for it and the server does not (say, working machine has .NET 3.5 SP1 and the server has .NET 3.5 without SP)
    • there's a difference in the .NET security config of the server that prevents reflection invoke - I doubt that, I think the error message would be different in this case.

    Try building this code on the server using the C# compile directly and then run the exe, see what happens.

    I'll move this to the CLR forums as it looks like a CLR problem, not BCL. Perhaps someone from Microsoft sees this and has an idea.

    Sunday, August 18, 2013 8:06 AM
    Moderator
  • Let me know if any more information is required for troubleshooting...
    Tuesday, August 20, 2013 4:19 PM