C
C#2mo ago
Kagano

how to get undefinied type in a <>

here a example what i want like to do:
Type type = ...;
SyncedVariable<typeof(type)> s = new SyncedVariable<typeof(type)>();
Type type = ...;
SyncedVariable<typeof(type)> s = new SyncedVariable<typeof(type)>();
14 Replies
333fred
333fred2mo ago
You cannot do that at compile time
Marvin
Marvin2mo ago
You can either do that with Reflection at runtime or change the approach a little and use code generators
Kagano
Kagano2mo ago
yeah i want a simular resoultuion
333fred
333fred2mo ago
You either need to make your context generic, or use runtime reflection
Kagano
Kagano2mo ago
i think i must store the type in a seperate variable in the SyncedVariable
Angius
Angius2mo ago
Just FYI, C# is not Typescript and you would benefit from not treating it as such Chances are it's an XY issue that can be solved with generics
Marvin
Marvin2mo ago
if you cant have the entire context generic, you have to at some point do something like this:
Kagano
Kagano2mo ago
have anyone a example?
Angius
Angius2mo ago
We don't really know the wider context of what you want to do, so hard to give an example
Marvin
Marvin2mo ago
C#
Type type = typeof(A);
Type genericType = typeof(B<>).MakeGenericType(type);
object isntance = Activator.CreateInstance(genericType)!;
C#
Type type = typeof(A);
Type genericType = typeof(B<>).MakeGenericType(type);
object isntance = Activator.CreateInstance(genericType)!;
very ugly
Angius
Angius2mo ago
public void Foo<T>()
{
Blah<T> s = new Blah<T>();
}
public void Foo<T>()
{
Blah<T> s = new Blah<T>();
}
would be ideal Or make it a generic class or w/e
Marvin
Marvin2mo ago
i approve of trying to get the entire context generic generally reflection is the last option u want to use
Angius
Angius2mo ago
ye
Marvin
Marvin2mo ago
if you explain more in what context you want to use it, we could maybe give you more tips
Want results from more Discord servers?
Add your server