C
C#3mo 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
333fred3mo ago
You cannot do that at compile time
Marvin
Marvin3mo ago
You can either do that with Reflection at runtime or change the approach a little and use code generators
Kagano
KaganoOP3mo ago
yeah i want a simular resoultuion
333fred
333fred3mo ago
You either need to make your context generic, or use runtime reflection
Kagano
KaganoOP3mo ago
i think i must store the type in a seperate variable in the SyncedVariable
Angius
Angius3mo 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
Marvin3mo ago
if you cant have the entire context generic, you have to at some point do something like this:
Kagano
KaganoOP3mo ago
have anyone a example?
Angius
Angius3mo ago
We don't really know the wider context of what you want to do, so hard to give an example
Marvin
Marvin3mo 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
Angius3mo 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
Marvin3mo ago
i approve of trying to get the entire context generic generally reflection is the last option u want to use
Angius
Angius3mo ago
ye
Marvin
Marvin3mo 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