Making a List<T> from an instance of T without knowing T
Is that possible? The idea is to make a List of T if the the object in question is T
34 Replies
Could you show some example?
Not sure how would you have an instance of
T
without knowing what the T
isim not sure what you are looking for exactly, but this is the answer I assume you would want something like
Since i assume you are working with generics?
if its not what you are looking for, then please can you provide more details?
Hey folks, already resolved this earlier with some changes in the approach. But used the following at some point:
i see. so you wanted to make it from reflection.
That's what I ended up with.
though I have a question. what areas would you need to make a list of type T via reflection? I want to see if theres a better way
can I see an example of its use case in your code?
I'm also curious what prompted you to resort to reflections
My initial problem is that at some point in the code I end up with a:
But the consuming class for this object has something like:
So, the idea was to automatically convert a Box() class if it's a singular object to List<Box> without knowing the type (Say this code is on a baseclass instead of each object
So what I did instead, is to make a List<Box> in the first place, instead of dealing with singular boxes.
And then at some point, I have to do:
where boxes can be a List<Box> object. Hence the reflection.
Uh,
?
yh this works. unless its not what you are looking for?
It's somewhat what I ended up with
a similar approach.
Imma be honest I don't fully understand what you're talking about
With "converting a box class if it's singular" and stuff
Its okay. HAhaha
what do you intend to do with the boxes
All good.
Think of it as forcing a singular result to a list instead.
Then... just have a list? And add to that list?
No clue what you mean
you want to make one result into a list? why? what way? is there something im missing
"Forcing a singular result to a list" is just making the result be a list
Like, instead of
return new Box()
you do return new List<Box>(){new Box()}
Yes. This is what I ended up with. On initial development, the data being queried was a singular result, but the consuming object of that data expects a list of those objects. Therefore the incompatiblity
No idea why reflections are involved
So I had the idea of "what if I convert this on the fly" etc.
If the objects expects a list give it a list...?
Like
???
That's what I ended up doing actually hahaha
why cant you just do
So am I to assume you threw away that reflections code? Good
it's supposed to be a lazy autoresolver, where if it sees that the object is just a singular object BUT the List expecting is composed of the object's type, then make a new list of the object's type so it would return a List. Get me? haha
say:
So you wanted, what,
List<IBox> boxes = new Box()
to be valid?
Because you can just List<IBox> boxes = [new Box()]
WAIT WHAT
Alright, ill check that out.
u need to have c# 12 for this though
Ohno
Angius
REPL Result: Success
Result: List<int>
Compile: 282.492ms | Execution: 27.961ms | React with ❌ to remove this embed.
Interesting
Will check that out, see if it fits.
In slightly older versions you'd use target-type
new
it doesnt only work for list. it works for every type that implements from IEnumerable<T>
you can also plop it in a method
removing the need to type new() makes it feel 10x better for some reason :kekw: