从构造函数获取参数



我正在尝试将构造函数的分配参数添加到列表中的列表中。

public class assignCells
{
List<List<Vector3>> Cells = new 
List<List<Vector3>>();
public assignCells(Vector3 bottom, Vector3 top, Vector3 right, Vector3 left)
{
Type Parm = typeof(assignCells);
Type[] VC = new Type[3];
VC[0] = typeof(Vector3);
VC[1] = typeof(Vector3);
VC[2] = typeof(Vector3);
VC[3] = typeof(Vector3);
ConstructorInfo Constructbase = Parm.GetConstructor(VC);
if (top.x == bottom.x + 20.0 && 
right.x == left.x + 50.0)
{
Cells.Add(Constructbase.GetParameters());
}
}
}

但是,此代码不起作用。最后一行只是说它无法从参数信息转换为向量列表。我真的很感激任何帮助,即使你必须告诉我我以完全错误的方式去做。

尝试以下操作:

public class assignCells
{
List<List<Vector3>> Cells = new List<List<Vector3>>();
public assignCells(Vector3 bottom, Vector3 top, Vector3 right, Vector3 left)
{
List<Vector3> newListVector3 = new List<Vector3>() { bottom, top, right, left };
Cells.Add(newListVector3);
}
}

我不太了解您的模型,但我建议您尝试此代码

public class AssignCells
{
List<List<ParameterInfo>> Cells = new List<List<ParameterInfo>>();
public AssignCells(Vector3 bottom, Vector3 top, Vector3 right, Vector3 left)
{
Type Parm = typeof(AssignCells);
Type[] VC = new Type[4];
VC[0] = typeof(Vector3);
VC[1] = typeof(Vector3);
VC[2] = typeof(Vector3);
VC[3] = typeof(Vector3);
var Constructbase = Parm.GetConstructor(VC);
if (top.x == bottom.x + 20.0 && right.x == left.x + 50.0)
{
Cells.Add(Constructbase.GetParameters().ToList());
}
}
}

Cells应该是ParameterInfo列表的列表,以避免编译错误。.ToList()来到这里是为了允许从Array转换为List希望有帮助!

最新更新