C
Join ServerC#
help
Type as Parameter, and string[] from Linq
TBTed Bunny9/16/2022
private static string[] GetRolls(AgentHitbox agentHitbox, Type type) =>
agentHitbox.agent.GetTraits<type>().Select(t => t.Rolls);
1. How can I use type this way? Typeof(type) didn't work either.2.
Rolls
is a string[]. I need to concatenate it from the series but I haven't done that in Linq before. How do?HHimmDawg9/16/2022
1.) Seems like you need generics for the function too
2.) What do you mean by concatenate? Adding the strings together? you can do that with
2.) What do you mean by concatenate? Adding the strings together? you can do that with
string.Join(" ", stringArray);
. You can replace " "
with any string you want to have in between each string in the array 
TBTed Bunny9/16/2022
1. How do I do that?
2. I need to return a string[] that is a concatenation of all the Traits' Rolls string[]s
2. I need to return a string[] that is a concatenation of all the Traits' Rolls string[]s
HHimmDawg9/16/2022
1.) I'd try
But at compile time you wont know if T has a property
GetRolls<T>(AgentHitbox agentHitbox) => agentHitbox.agent.GetTraits<T> ...
But at compile time you wont know if T has a property
Rolls
so you have to restrict T to something you know, soooprivate static string[] GetRolls<T>(AgentHitbox agentHitbox) where T: SomeTypeWithRolls
{
return agentHitbox.agent.GetTraits<T>().Select(t => t.Rolls).ToArray();
}
HHimmDawg9/16/2022
2.) You are using Linq, so you can use
Aggregate
or i believe SelectMany
could work tooEEro9/16/2022
And don't forget ToArray at the end
TBTed Bunny9/16/2022
I'll give that a shot, thanks!
TBTed Bunny9/16/2022
Umm
You guys rule

OOrannis9/16/2022
In general, if you find yourself using a
System.Type
parameter, you've probably done something wrong 🙂TBTed Bunny9/16/2022
Ah, ok. I'll keep that in mind, thank you!