Hi, i have a c# stack reference a nodejs stack, i ...
# general
c
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
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
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.csline 44 at object RuntimeType.CreateInstanceOfT() --- End of inner exception stack trace ---
e
I meant cast both so like
Copy code
(string)(
   (ImmutableArray<object>)((await networkStackRef.GetOutputDetailsAsync("eksVpcPrivateSubnetIds")).Value))
c
严重性 代码 说明 项目 文件 行 禁止显示状态 错误 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
Oh ops missed the indexer 🤦
Copy code
(string)(
   ((ImmutableArray<object>)(
      (await networkStackRef.GetOutputDetailsAsync("eksVpcPrivateSubnetIds")).Value
   ))[0]
)
c
i'm new to csharp, why i can't castArray<string>, if i need the subnetIds array, how to do it?
e
oh you could, if you need the whole array
Copy code
((ImmutableArray<object>)(
      (await networkStackRef.GetOutputDetailsAsync("eksVpcPrivateSubnetIds")).Value
   )).CastArray<string>()
c
I have tried the code, but got these error
Copy code
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
Try
As
instead of
CastArray
c
Compiler Error CS0039
e
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:
Copy code
ImmutableArray.Create(((ImmutableArray<object>)(await networkStackRef.GetOutputDetailsAsync("eksVpcPrivateSubnetIds")).Value).Cast<string>())
Make sure
using System.Linq;
is at the top of the file.
c
Copy code
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.csline 57 at object RuntimeType.CreateInstanceOfT() --- End of inner exception stack trace ---
Copy code
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
We'll probably do a Rust SDK at some point, casts are always tricky