Pikay421
Pikay421
CC#
Created by Pikay421 on 4/21/2023 in #help
❔ 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();
8 replies