Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Passing object[] Elements as Parameters to Invoke()

Passing object[] Elements as Parameters to Invoke()

Scheduled Pinned Locked Moved C#
questiondesignalgorithmshelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ezazazel
    wrote on last edited by
    #1

    Hi guys! Maybe anyone can help me with this: I have a object[] with values e.g.:

    object[] obj = new object[]{"a",1};

    Furthermore I have a dynamic method which is in reality for example a

    Func function;

    Is there a way to use the values of the object[] as parameters for the function? Or how can I invoke the method with the values from the object[]? ----------------- Why is this needed? I have classes which store Func<1..n> within a dynamic property e.g.

    class A
    public dynamic Algorithm;
    Algorithm = new System.Func((x,y) => { return x + y; });

    class B
    public dynamic Algorithm;
    Algorithm = new System.Func((x,y) => { return x / y * z; });

    . By iterating through MethodInfo[] I get

    Type inputType = parameter.ParameterType;
    Type returnType = member.ReturnType;

    and can therefore create my UI. Now I can create objects out of this by using the Activator

    object inputParameterObject = Activator.CreateInstance(inputType);

    In the UI I fill in the values.

    inputParameterObject1 = a;
    inputParameterObject2 = 1;

    The inputParameterObjects are stored in an object[] Now I need to invoke the Func<1..n> by passing the values from the UI.

    Algorithm.Invoke(inputParameterObjectArray);

    does not work as it is not equal to

    Algorithm.Invoke("a",1);

    Or in other words

    Algorithm.Invoke(object[]) != Algorithm.Invoke(string,int) || Algorithm.Invoke(object,object)

    Help would be appreciated! So long,

    M 1 Reply Last reply
    0
    • E ezazazel

      Hi guys! Maybe anyone can help me with this: I have a object[] with values e.g.:

      object[] obj = new object[]{"a",1};

      Furthermore I have a dynamic method which is in reality for example a

      Func function;

      Is there a way to use the values of the object[] as parameters for the function? Or how can I invoke the method with the values from the object[]? ----------------- Why is this needed? I have classes which store Func<1..n> within a dynamic property e.g.

      class A
      public dynamic Algorithm;
      Algorithm = new System.Func((x,y) => { return x + y; });

      class B
      public dynamic Algorithm;
      Algorithm = new System.Func((x,y) => { return x / y * z; });

      . By iterating through MethodInfo[] I get

      Type inputType = parameter.ParameterType;
      Type returnType = member.ReturnType;

      and can therefore create my UI. Now I can create objects out of this by using the Activator

      object inputParameterObject = Activator.CreateInstance(inputType);

      In the UI I fill in the values.

      inputParameterObject1 = a;
      inputParameterObject2 = 1;

      The inputParameterObjects are stored in an object[] Now I need to invoke the Func<1..n> by passing the values from the UI.

      Algorithm.Invoke(inputParameterObjectArray);

      does not work as it is not equal to

      Algorithm.Invoke("a",1);

      Or in other words

      Algorithm.Invoke(object[]) != Algorithm.Invoke(string,int) || Algorithm.Invoke(object,object)

      Help would be appreciated! So long,

      M Offline
      M Offline
      Matt T Heffron
      wrote on last edited by
      #2

      I haven't tried it, but could you use the .DynamicInvoke method instead? Its signature looks like what you want.

      E 1 Reply Last reply
      0
      • M Matt T Heffron

        I haven't tried it, but could you use the .DynamicInvoke method instead? Its signature looks like what you want.

        E Offline
        E Offline
        ezazazel
        wrote on last edited by
        #3

        Thank you! Seems to work (just tried it with a short piece of code - going to check it within my project later).

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups