Dynamically instantiate class and call function

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

    Dynamically instantiate class and call function

    Hi,

    can I create a instance from a class which is another assembly and call a
    function inside this assembly? I know the assembly name, class name, the
    function name and its signature.

    Thanks
    Christian
  • Vladimir Matveev

    #2
    Re: Dynamically instantiate class and call function

    yes, you can

    Assembly otherAssembly = Assembly.Load(. ..);

    Type type = otherAssemby.Ge tType(typename) ;

    object obj = otherAssembly.C reateInstance(t ype);

    BindingFlags bf = BindingFlags.In vokeMethod | BindingFlags.In stance |
    BindingFlags.Pu blic | BindingFlags.No nPublic;

    type.InvokeMemb er("methodname" , bf, null, obj, new object[] {arg1,
    arg2});

    // or
    ISomeWellKnownI nterface ifc =
    (ISomeWellKnown Interface)other Assembly.Create Instance(typena me);
    ifc.methodname( arg1, arg2);

    Comment

    • Marc Gravell

      #3
      Re: Dynamically instantiate class and call function

      Something like this?

      Assembly a = Assembly.LoadWi thPartialName(" MyAssembly");
      object obj = a.CreateInstanc e("MyType");
      Type[] signature = new Type[] {typeof(int), typeof(string)} ;
      MethodInfo method = obj.GetType().G etMethod("MyMet hod",
      signature);
      object[] parameters = new object[] { 123, "abc" };
      method.Invoke(o bj, parameters);

      Note: if you can, I would advise working against an interface, i.e. make
      both MyAssembly and the above code reference a (single) separate assembly
      with IMyInterface {void MyMethod(int value1, string value2);} - you can then
      just case obj to IMyInterface and you don't need anywhere near as much
      reflection.

      Marc


      Comment

      • Christian Havel

        #4
        Re: Dynamically instantiate class and call function

        Hi Vladimir,

        thanks for your help.

        Bye Christian

        "Vladimir Matveev" schrieb:
        [color=blue]
        > yes, you can
        >
        > Assembly otherAssembly = Assembly.Load(. ..);
        >
        > Type type = otherAssemby.Ge tType(typename) ;
        >
        > object obj = otherAssembly.C reateInstance(t ype);
        >
        > BindingFlags bf = BindingFlags.In vokeMethod | BindingFlags.In stance |
        > BindingFlags.Pu blic | BindingFlags.No nPublic;
        >
        > type.InvokeMemb er("methodname" , bf, null, obj, new object[] {arg1,
        > arg2});
        >
        > // or
        > ISomeWellKnownI nterface ifc =
        > (ISomeWellKnown Interface)other Assembly.Create Instance(typena me);
        > ifc.methodname( arg1, arg2);
        >
        >[/color]

        Comment

        • Christian Havel

          #5
          Re: Dynamically instantiate class and call function

          Hi Marc,

          thanks for your help and regards from Munich.

          Christian

          "Marc Gravell" schrieb:
          [color=blue]
          > Something like this?
          >
          > Assembly a = Assembly.LoadWi thPartialName(" MyAssembly");
          > object obj = a.CreateInstanc e("MyType");
          > Type[] signature = new Type[] {typeof(int), typeof(string)} ;
          > MethodInfo method = obj.GetType().G etMethod("MyMet hod",
          > signature);
          > object[] parameters = new object[] { 123, "abc" };
          > method.Invoke(o bj, parameters);
          >
          > Note: if you can, I would advise working against an interface, i.e. make
          > both MyAssembly and the above code reference a (single) separate assembly
          > with IMyInterface {void MyMethod(int value1, string value2);} - you can then
          > just case obj to IMyInterface and you don't need anywhere near as much
          > reflection.
          >
          > Marc
          >
          >
          >[/color]

          Comment

          • Christian Havel

            #6
            Re: Dynamically instantiate class and call function

            Hi,

            it works! But how can I solve this problem if the assembly is located not in
            the GAC but in the application directory?

            Thanks
            Christian

            "Vladimir Matveev" schrieb:
            [color=blue]
            > yes, you can
            >
            > Assembly otherAssembly = Assembly.Load(. ..);
            >
            > Type type = otherAssemby.Ge tType(typename) ;
            >
            > object obj = otherAssembly.C reateInstance(t ype);
            >
            > BindingFlags bf = BindingFlags.In vokeMethod | BindingFlags.In stance |
            > BindingFlags.Pu blic | BindingFlags.No nPublic;
            >
            > type.InvokeMemb er("methodname" , bf, null, obj, new object[] {arg1,
            > arg2});
            >
            > // or
            > ISomeWellKnownI nterface ifc =
            > (ISomeWellKnown Interface)other Assembly.Create Instance(typena me);
            > ifc.methodname( arg1, arg2);
            >
            >[/color]

            Comment

            • Vladimir Matveev

              #7
              Re: Dynamically instantiate class and call function

              You also can subscribe to AppDomain.Assem blyResolve event and locate it
              manually

              Comment

              • Ignacio Machin \( .NET/ C# MVP \)

                #8
                Re: Dynamically instantiate class and call function

                Hi,

                "Christian Havel" <ChristianHavel @discussions.mi crosoft.com> wrote in
                message news:A47054B3-E805-4B44-8076-0B8674545210@mi crosoft.com...[color=blue]
                > Hi,
                >
                > it works! But how can I solve this problem if the assembly is located not
                > in
                > the GAC but in the application directory?[/color]

                Assembly.LoadFr om will do it


                --
                Ignacio Machin,
                ignacio.machin AT dot.state.fl.us
                Florida Department Of Transportation


                Comment

                • Christian Havel

                  #9
                  Re: Dynamically instantiate class and call function

                  Hi Vladimir,

                  thanks a lot.
                  Christian

                  "Vladimir Matveev" schrieb:
                  [color=blue]
                  > You also can subscribe to AppDomain.Assem blyResolve event and locate it
                  > manually
                  >
                  >[/color]

                  Comment

                  • Christian Havel

                    #10
                    Re: Dynamically instantiate class and call function

                    Hi Ignacio,

                    thanks a lot and regards from Munich.

                    Christian

                    "Ignacio Machin ( .NET/ C# MVP )" schrieb:
                    [color=blue]
                    > Hi,
                    >
                    > "Christian Havel" <ChristianHavel @discussions.mi crosoft.com> wrote in
                    > message news:A47054B3-E805-4B44-8076-0B8674545210@mi crosoft.com...[color=green]
                    > > Hi,
                    > >
                    > > it works! But how can I solve this problem if the assembly is located not
                    > > in
                    > > the GAC but in the application directory?[/color]
                    >
                    > Assembly.LoadFr om will do it
                    >
                    >
                    > --
                    > Ignacio Machin,
                    > ignacio.machin AT dot.state.fl.us
                    > Florida Department Of Transportation
                    >
                    >
                    >[/color]

                    Comment

                    Working...