C
C#4mo ago
{sus}

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
Angius
Angius4mo ago
Could you show some example? Not sure how would you have an instance of T without knowing what the T is
Izagawd
Izagawd4mo ago
im not sure what you are looking for exactly, but this is the answer I assume you would want something like
public static List<T> MakeListFrom<T>(){
return new List<T>();
}
public static List<T> MakeListFrom<T>(){
return new List<T>();
}
Since i assume you are working with generics? if its not what you are looking for, then please can you provide more details?
{sus}
{sus}OP4mo ago
Hey folks, already resolved this earlier with some changes in the approach. But used the following at some point:
public static IList CreateListFromType(Type elementType)
{
if (elementType == null)
throw new ArgumentNullException(nameof(elementType));

Type listType = typeof(List<>).MakeGenericType(elementType);

return (IList)Activator.CreateInstance(listType);
}
public static IList CreateListFromType(Type elementType)
{
if (elementType == null)
throw new ArgumentNullException(nameof(elementType));

Type listType = typeof(List<>).MakeGenericType(elementType);

return (IList)Activator.CreateInstance(listType);
}
Izagawd
Izagawd4mo ago
i see. so you wanted to make it from reflection.
{sus}
{sus}OP4mo ago
That's what I ended up with.
Izagawd
Izagawd4mo ago
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?
Angius
Angius4mo ago
I'm also curious what prompted you to resort to reflections
{sus}
{sus}OP4mo ago
My initial problem is that at some point in the code I end up with a:
Box boxA = new Box();
Box boxA = new Box();
But the consuming class for this object has something like:
class Warehouse{

List<Box> boxes;

}
class Warehouse{

List<Box> boxes;

}
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:
List<IBox> boxes
List<IBox> boxes
where boxes can be a List<Box> object. Hence the reflection.
Angius
Angius4mo ago
Uh,
class Warehouse<TBox>
{
public TBox SingleBox { get; set; }
public List<TBox> AllBoxes { get; set; }
}
class Warehouse<TBox>
{
public TBox SingleBox { get; set; }
public List<TBox> AllBoxes { get; set; }
}
?
Izagawd
Izagawd4mo ago
yh this works. unless its not what you are looking for?
{sus}
{sus}OP4mo ago
It's somewhat what I ended up with a similar approach.
Angius
Angius4mo ago
Imma be honest I don't fully understand what you're talking about With "converting a box class if it's singular" and stuff
{sus}
{sus}OP4mo ago
Its okay. HAhaha
Izagawd
Izagawd4mo ago
what do you intend to do with the boxes
{sus}
{sus}OP4mo ago
All good. Think of it as forcing a singular result to a list instead.
Angius
Angius4mo ago
Then... just have a list? And add to that list? No clue what you mean
Izagawd
Izagawd4mo ago
you want to make one result into a list? why? what way? is there something im missing
Angius
Angius4mo ago
"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()}
{sus}
{sus}OP4mo ago
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
Angius
Angius4mo ago
No idea why reflections are involved
{sus}
{sus}OP4mo ago
So I had the idea of "what if I convert this on the fly" etc.
Angius
Angius4mo ago
If the objects expects a list give it a list...? Like ???
{sus}
{sus}OP4mo ago
That's what I ended up doing actually hahaha
Izagawd
Izagawd4mo ago
why cant you just do
queryResult.FirstOrDefault();
queryResult.FirstOrDefault();
Angius
Angius4mo ago
So am I to assume you threw away that reflections code? Good
{sus}
{sus}OP4mo ago
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:
List<IBox> boxes = [Do convert single box to List<Box>]
List<IBox> boxes = [Do convert single box to List<Box>]
Angius
Angius4mo ago
So you wanted, what, List<IBox> boxes = new Box() to be valid? Because you can just List<IBox> boxes = [new Box()]
{sus}
{sus}OP4mo ago
WAIT WHAT Alright, ill check that out.
Izagawd
Izagawd4mo ago
u need to have c# 12 for this though
{sus}
{sus}OP4mo ago
Ohno
MODiX
MODiX4mo ago
Angius
REPL Result: Success
List<int> ints = [1];
ints
List<int> ints = [1];
ints
Result: List<int>
[
1
]
[
1
]
Compile: 282.492ms | Execution: 27.961ms | React with ❌ to remove this embed.
{sus}
{sus}OP4mo ago
Interesting Will check that out, see if it fits.
Angius
Angius4mo ago
In slightly older versions you'd use target-type new
List<int> ints = new(){ 1 };
List<int> ints = new(){ 1 };
Izagawd
Izagawd4mo ago
it doesnt only work for list. it works for every type that implements from IEnumerable<T>
int[] a = [1,2,3,4];
HashSet<SomeClass> b = [new SomeClass(), new SomeClass()]
int[] a = [1,2,3,4];
HashSet<SomeClass> b = [new SomeClass(), new SomeClass()]
you can also plop it in a method
public void SomeMethod(List<int> idk){}



SomeMethod([1,2,3,4]);
public void SomeMethod(List<int> idk){}



SomeMethod([1,2,3,4]);
removing the need to type new() makes it feel 10x better for some reason :kekw:
Want results from more Discord servers?
Add your server