https://pulumi.com logo
Title
c

cuddly-angle-21517

04/25/2023, 8:20 AM
Hi, i have a c# stack reference a nodejs stack, i stuck at when get the reference the nodejs stack's output, i just write below code to solve the type cast, any better method?
e

echoing-dinner-19531

04/25/2023, 8:26 AM
You could avoid the
apply
call with outputDetails. I think you should be able to cast directly to
ImmutableArray<object>
and then pull out the inner object and cast that to a string. Shouldn't have to go through the whole JSON serialise/deserialise loop. Def something we want to make better here though.
c

cuddly-angle-21517

04/25/2023, 8:44 AM
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. at object RuntimeType.CreateInstanceOfT() at T Activator.CreateInstance<T>() at Task<int> Pulumi.Deployment+Runner.Pulumi.IRunner.RunAsync<TStack>(IServiceProvider serviceProvider)+() => { } at Task<int> Pulumi.Deployment+Runner.RunAsync<TStack>(Func<TStack> stackFactory) ---> System.InvalidCastException: Unable to cast object of type 'System.Object[]' to type 'System.String[]'. at ImmutableArray<TOther> System.Collections.Immutable.ImmutableArray<T>.CastArray<TOther>() at new UgcPubInfraStack() in E😕codes/wired/wired-ugc-pub-infra/UgcPubInfraStack.cs:line 44 at object RuntimeType.CreateInstanceOfT() --- End of inner exception stack trace ---
e

echoing-dinner-19531

04/25/2023, 8:47 AM
I meant cast both so like
(string)(
   (ImmutableArray<object>)((await networkStackRef.GetOutputDetailsAsync("eksVpcPrivateSubnetIds")).Value))
c

cuddly-angle-21517

04/25/2023, 8:50 AM
严重性 代码 说明 项目 文件 行 禁止显示状态 错误 CS0030 无法将类型“System.Collections.Immutable.ImmutableArray<object>”转换为“string” wired-ugc-pub-infra E:\codes\wired\wired-ugc-pub-infra\UgcPubInfraStack.cs 53 活动
this chinese is means can't cast System.Collections.Immutable.ImmutableArray<object> to string
e

echoing-dinner-19531

04/25/2023, 8:52 AM
Oh ops missed the indexer 🤦
(string)(
   ((ImmutableArray<object>)(
      (await networkStackRef.GetOutputDetailsAsync("eksVpcPrivateSubnetIds")).Value
   ))[0]
)
c

cuddly-angle-21517

04/25/2023, 8:57 AM
i'm new to csharp, why i can't castArray<string>, if i need the subnetIds array, how to do it?
e

echoing-dinner-19531

04/25/2023, 9:03 AM
oh you could, if you need the whole array
((ImmutableArray<object>)(
      (await networkStackRef.GetOutputDetailsAsync("eksVpcPrivateSubnetIds")).Value
   )).CastArray<string>()
c

cuddly-angle-21517

04/25/2023, 9:05 AM
I have tried the code, but got these error
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
       at object RuntimeType.CreateInstanceOfT()
       at T Activator.CreateInstance<T>()
       at Task<int> Pulumi.Deployment+Runner.Pulumi.IRunner.RunAsync<TStack>(IServiceProvider serviceProvider)+() => { }
       at Task<int> Pulumi.Deployment+Runner.RunAsync<TStack>(Func<TStack> stackFactory) ---> System.InvalidCastException: Unable to cast object of type 'System.Object[]' to type 'System.String[]'.
       at ImmutableArray<TOther> System.Collections.Immutable.ImmutableArray<T>.CastArray<TOther>()
       at new UgcPubInfraStack() in E:/codes/wired/wired-ugc-pub-infra/UgcPubInfraStack.cs:line 59
       at object RuntimeType.CreateInstanceOfT()
       --- End of inner exception stack trace ---
e

echoing-dinner-19531

04/25/2023, 9:13 AM
Try
As
instead of
CastArray
c

cuddly-angle-21517

04/25/2023, 9:16 AM
Compiler Error CS0039
e

echoing-dinner-19531

04/25/2023, 9:26 AM
Oh 😞 one sec let me load up visual studio.
Right ok the Cast methods on ImmutableArray are for direct array casts, not per element casts so you need to use Linq's cast method for this:
ImmutableArray.Create(((ImmutableArray<object>)(await networkStackRef.GetOutputDetailsAsync("eksVpcPrivateSubnetIds")).Value).Cast<string>())
Make sure
using System.Linq;
is at the top of the file.
c

cuddly-angle-21517

04/25/2023, 9:44 AM
var subnetIds = ImmutableArray.Create(((ImmutableArray<object>)(await networkStackRef.GetOutputDetailsAsync("eksVpcPrivateSubnetIds")).Value).Cast<string>());
System.Console.WriteLine(subnetIds.First());
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. at object RuntimeType.CreateInstanceOfT() at T Activator.CreateInstance<T>() at Task<int> Pulumi.Deployment+Runner.Pulumi.IRunner.RunAsync<TStack>(IServiceProvider serviceProvider)+() => { } at Task<int> Pulumi.Deployment+Runner.RunAsync<TStack>(Func<TStack> stackFactory) ---> System.InvalidCastException: Unable to cast object of type 'System.Collections.Immutable.ImmutableArray`1[System.Object]' to type 'System.String'. at IEnumerable<TResult> System.Linq.Enumerable.CastIterator<TResult>(IEnumerable source)+MoveNext() at TSource System.Linq.Enumerable.TryGetFirst<TSource>(IEnumerable<TSource> source, out bool found) at TSource System.Linq.Enumerable.First<TSource>(IEnumerable<TSource> source) at new UgcPubInfraStack() in E😕codes/wired/wired-ugc-pub-infra/UgcPubInfraStack.cs:line 57 at object RuntimeType.CreateInstanceOfT() --- End of inner exception stack trace ---
var array = ((ImmutableArray<object>)networkingStackRef
            .GetOutputDetailsAsync("eksVpcPrivateSubnetIds").Result.Value!).Cast<string>().ToArray();
the code above is ok
I prefer rust to c#.....terrible type cast...
e

echoing-dinner-19531

04/25/2023, 10:10 AM
We'll probably do a Rust SDK at some point, casts are always tricky