C
C#•2y ago
Ted Bunny

Type as Parameter, and string[] from Linq

private static string[] GetRolls(AgentHitbox agentHitbox, Type type) =>
agentHitbox.agent.GetTraits<type>().Select(t => t.Rolls);
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?
7 Replies
HimmDawg
HimmDawg•2y ago
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 string.Join(" ", stringArray);. You can replace " " with any string you want to have in between each string in the array owo
Ted Bunny
Ted Bunny•2y ago
1. How do I do that? 2. I need to return a string[] that is a concatenation of all the Traits' Rolls string[]s
HimmDawg
HimmDawg•2y ago
1.) I'd try 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, sooo
private static string[] GetRolls<T>(AgentHitbox agentHitbox) where T: SomeTypeWithRolls
{
return agentHitbox.agent.GetTraits<T>().Select(t => t.Rolls).ToArray();
}
private static string[] GetRolls<T>(AgentHitbox agentHitbox) where T: SomeTypeWithRolls
{
return agentHitbox.agent.GetTraits<T>().Select(t => t.Rolls).ToArray();
}
2.) You are using Linq, so you can use Aggregate or i believe SelectMany could work too
ero
ero•2y ago
And don't forget ToArray at the end
Ted Bunny
Ted Bunny•2y ago
I'll give that a shot, thanks! Umm catthinking You guys rule
333fred
333fred•2y ago
In general, if you find yourself using a System.Type parameter, you've probably done something wrong 🙂
Ted Bunny
Ted Bunny•2y ago
Ah, ok. I'll keep that in mind, thank you!
Want results from more Discord servers?
Add your server
More Posts