Skip to content

Support variadic parameters for .NET #153

Closed

Description

@mpiroc

This might be more involved that just adding the "params" keyword. Consider the following signature:

public void DoSomething(params object[] myParam)
{
}

// ...

object[] foo = // ...
// Because an object[] is also an object, the compiler treats foo as the first element
// of the params array, rather than the params array itself.
DoSomething(foo);

But in this case, there's no ambiguity:

public void DoSomething(params string[] myParam)
{
}

// ...

string[] foo = // ...

// The compiler treats foo as the params array, rather than the first element of the params array.
DoSomething(foo);

This might not matter, since generated code does not include any invocations of the generated methods. Just something to keep in mind.

Activity

  1. added
    language/dotnet
    good first issue
    on Aug 11, 2018
  2. added
    feature-request
    and removed on Jan 3, 2019
  3. added
    effort/medium
    on Apr 22, 2019
  4. McDoit commented on Apr 25, 2019

    @McDoit

    I guess i'm running into a related issue to this now in 0.29 of the C# CDK

    AddActions of the PolicyStatement has the following signature
    [JsiiMethod("addActions", "{\"type\":{\"fqn\":\"@aws-cdk/aws-iam.PolicyStatement\"}}", "[{\"name\":\"actions\",\"variadic\":true,\"type\":{\"primitive\":\"string\"}}]", false, false)]

    But the generated C# method has the following signature, with a string
    public virtual PolicyStatement AddActions(string actions)

    Is there anyway i can create a correct string of a string params, or is this just something that need to be solved in this issue?

  5. bfeinb commented on Apr 25, 2019

    @bfeinb
  6. McDoit commented on Apr 26, 2019

    @McDoit

    This has been an issue for a while. The way I deal with this situation is to create an extension method to get the behavior I want. For my purposes I wanted to add multiple actions via a comma delimited set of actions as a single string, but you could easily go with an IEnumerable instead. The extension method I use is included below. It would be better to teach JSII how to deal with variable number of arguments in C#. Unfortunately I’m slammed on other projects at the moment, or I’d look at contributing something.
    …
    --Bonnie ///

    /// Adds actions to a policy statement from a comma separated list of actions names as part of initializing IAM objects. /// /// What policy statement /// The names of the actions to add /// public static PolicyStatement AddActionsFromString(this PolicyStatement statement, string actions) { var policyStatement = statement; foreach (string action in actions.Split(", ")) { policyStatement = policyStatement.AddAction(action); } return policyStatement; } From: Erik Karlsson [email protected] Sent: Thursday, April 25, 2019 6:15 AM To: awslabs/jsii [email protected] Cc: Feinberg, Bonnie [email protected]; Manual [email protected] Subject: Re: [awslabs/jsii] Support variadic parameters for .NET (#153) I guess i'm running into a related issue to this now in 0.29 of the C# CDK AddActions of the PolicyStatement has the following signature [JsiiMethod("addActions", "{"type":{"fqn":"@aws-cdk/aws-iam.PolicyStatement"}}", "[{"name":"actions","variadic":true,"type":{"primitive":"string"}}]", false, false)] But the generated C# method has the following signature, with a string public virtual PolicyStatement AddActions(string actions) Is there anyway i can create a correct string of a string params, or is this just something that need to be solved in this issue? — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub<#153 (comment)>, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AKCL62L6TAIWZTQYUNEVUXTPSGVGDANCNFSM4FOENHGQ.

    Thank you @bfeinb

    I tried something similar with a static method and looping over several actions, but then i get the following error in the app.Run() step:

    Amazon.JSII.Runtime.JsiiException: 'While synthesizing mimer-ses-15/db-key/Resource: System.ArgumentException: Could not convert value 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' with unrecognized type
    Parameter name: value
    

    Only reason i need it is beacuase of a related issue in the new 0.29, aws/aws-cdk#2375
    I guess i will have to wait for that, or just create a new statement for every action instead

  7. added this to the milestone on Jul 30, 2019
  8. added a commit that references this issue on Aug 9, 2019
    e8b5a35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumfeature-requestgood first issuelanguage/dotnetp0

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions