Problem with reflection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Skandy

    #1

    Problem with reflection

    Hello All:

    I'm having this little trouble getting a form displayed using
    reflection.

    objForm.GetType ().GetMethod("S howDialog",
    BindingFlags.In vokeMethod).Inv oke(objForm, null);

    The above is the line of code that is causing this trouble. I'm getting
    a NullreferenceEx ception at this line.

    I have debugged and checked that objForm is fine and is not null, and
    the MethodInfo is correct too and is not null again.
    Once I get the MethodInfo I try the Invoke(object, params);

    Because the ShowDialog method for the Form class doesnt accept any
    parameters, I pass a null value here.

    Can anyone point why I'm getting this NullReferenceEx ception?

    Thanks In Advance.

    Skandy.

  • Yury

    #2
    Re: Problem with reflection

    I checked your code and MethodInfo is null. Use other overload of
    GetMethod method to find ShowDialog:

    Type t = objForm.GetType ();
    MethodInfo m = t.GetMethod("Sh owDialog", new Type[0]);
    m.Invoke(objFor m, null);

    Comment

    • Skandy

      #3
      Re: Problem with reflection

      Hi Yury, Thanks for the reply.

      Yes I relaized it just now, that the MethodInfo is returned NULL and
      that explains why I get a NullreferenceEx ception.

      But why is that I get the following exception against this code:
      MethodInfo mi = objForm.GetType ().GetMethod("S how");

      Form is not null
      SmartClientAsse mbly.frmDisplay , Text: Reference Implementation for
      Smart Clients

      Ambiguous match found.
      mscorlib


      at System.RuntimeT ype.GetMethodIm pl(String name, BindingFlags
      bindingAttr, Bi
      nder binder, CallingConventi ons callConv, Type[] types,
      ParameterModifi er[] modi
      fiers)
      at System.Type.Get Method(String name)
      at ReferenceImplem entationSmartCl ients.Program.M ain(String[] args)
      in Z:\Deve
      lopment\RC1\Ref erenceImplement ationSmartClien ts\ReferenceImp lementationSmar tClie
      nts\Program.cs: line 34
      Press any key to continue . . .

      TIA.
      Skandy

      Comment

      • Yury

        #4
        Re: Problem with reflection

        You get this error because runtime doesn't know what method you need
        Show(IWin32Wind ows) or Show without parameters. So you need to specify
        parameters types to help runtime to resolve method. If you need Show()
        just pass empty array of Type as second parameter of GetMethod like in
        first case.

        GetMethod("Show ", new Type[0]);

        Comment

        • Skandy

          #5
          Re: Problem with reflection

          Thanks a lot, Yury

          Comment

          • Skandy

            #6
            Re: Problem with reflection

            Thanks a lot, Yury

            Comment

            Working...