C
C#2y ago
Pikay421

❔ Getting Inherited Generic-Typed Inner Classes With Reflection

If I have a class layout like this... By the way the Vector3Int type is from Unity but just know that its axes can be indexed like shown below
public abstract class Operator { }
public abstract class Operator<AType, BType, CType, ACalc, BCalc, CCalc> : Operator
{
public abstract CCalc Operate(ACalc a, BCalc b);
public abstract CType Combine(CCalc[] separated);
public abstract CCalc[] Separate(CType combined);
}
public abstract class IntComposedOperator<ABCType> : Operator<ABCType, ABCType, ABCType, int, int, int>
{
public class Add : IntComposedOperator<ABCType> { public override int Operate(int a, int b) => a + b; }
public class Subtract : IntComposedOperator<ABCType> { public override int Operate(int a, int b) => a - b; }
public override ABCType Combine(int[] separated) => default;
public override int[] Separate(ABCType combined) => default;
}
public abstract class Vector3IntOperator : IntComposedOperator<Vector3Int>
{
public override Vector3Int Combine(int[] separated) => new Vector3Int(separated[0], separated[1], separated[2]);
public override int[] Separate(Vector3Int combined) => new int[] { combined.x, combined.y, combined.z };
}
public abstract class Operator { }
public abstract class Operator<AType, BType, CType, ACalc, BCalc, CCalc> : Operator
{
public abstract CCalc Operate(ACalc a, BCalc b);
public abstract CType Combine(CCalc[] separated);
public abstract CCalc[] Separate(CType combined);
}
public abstract class IntComposedOperator<ABCType> : Operator<ABCType, ABCType, ABCType, int, int, int>
{
public class Add : IntComposedOperator<ABCType> { public override int Operate(int a, int b) => a + b; }
public class Subtract : IntComposedOperator<ABCType> { public override int Operate(int a, int b) => a - b; }
public override ABCType Combine(int[] separated) => default;
public override int[] Separate(ABCType combined) => default;
}
public abstract class Vector3IntOperator : IntComposedOperator<Vector3Int>
{
public override Vector3Int Combine(int[] separated) => new Vector3Int(separated[0], separated[1], separated[2]);
public override int[] Separate(Vector3Int combined) => new int[] { combined.x, combined.y, combined.z };
}
How would I use reflection to get an array of the "Add" and "Subtract" classes specifically dervied from Vector3IntOperator? I've tried doing this but it doesn't seem to work... (This bit uses some linq aswell)
public static Type[] GetDerivedTypes(this Type type, bool includeGenerics = true) => type.Assembly.GetTypes().Where((t) =>
{
return t.IsSubclassOf(type) && (includeGenerics || t.ContainsGenericParameters == false);
}).ToArray();
public static Type[] GetDerivedTypes(this Type type, bool includeGenerics = true) => type.Assembly.GetTypes().Where((t) =>
{
return t.IsSubclassOf(type) && (includeGenerics || t.ContainsGenericParameters == false);
}).ToArray();
2 Replies
JakenVeina
JakenVeina2y ago
it might be that .IsSubclassOf() only checks for DIRECT parent/child relationships not the full ancestry the docs page isn't really clear on that, one way or the other try swapping to .IsAssignableFrom() the docs DO say explicitly that Assembly.GetTypes() should include all nested and non-public types, so that's not the issue otherwise, you're just gonna have to crack down and debug it
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server