问题:当我在父类中调用 ValidateDynData 时,程序流不会转到 ValidateDynData 的子类实现。
我使用反射创建了我的类的实例。当我从另一个项目调用子类中的方法时,它会以子类中的正确方法(而不是父类的同名方法)结束,因此看起来设置正确。
这是反射部分在我的其他项目/类中的样子:
**注 2013 年 3 月 7 日:我添加了更多信息,以便您对此有所了解。它获取盒子的数量,循环通过盒子的数量,并为每个盒子创建一个控件并向表单添加一个选项卡。这是主要的CTool Visual Studio项目,是项目中的一个类,是一个表单。当我按下表单上的一个按钮时,带有我稍后将要创建的子类的信息(选定),它转到这个方法,CreatTabs():
cb = new CB();
int errValue = cb.FindUsbHid(ref HWndBoxID); //see how many boxes there are
if (errValue == 0 && HWndBoxID[0, 1] != 0)
{
for (int i = 0; i < cb.cbInfos.Length; i++)
{
if (controls[i] == null)
{
CB cb1 = new CB(); //need one for each box found or concurrent programming will fail
errValue = cb1.FindUsbHid(ref HWndBoxID); //need to do for each box to get all info
/////////////////////////////////////////
if (errValue == 0)
{
_assembly = Assembly.LoadFrom(programDll);
_type = _assembly.GetType("CrWriter.PC");
_objectInstance = Activator.CreateInstance(_type);
_parameters = new Object[] { cb1, programDll, templateArr, itsDll, cert, i, cb1.cbInfos[i].boxID };
controls[i] = new Control();
//The following lands in my child's GetPC method
//My parent also has a method called GetPC and that is called from the child.
//Then, most of the program flow is in the parent until I need to call ValidateDynData,
//discussed below
controls[i] = (Control)_type.InvokeMember("GetPC", BindingFlags.Default | BindingFlags.InvokeMethod, null, _objectInstance, _parameters);
controls[i].Dock = DockStyle.None;
this.Controls.Add(controls[i]);
TabPage newPage = new TabPage(string.Format("{0}:{1}", cb1.cbInfos[i].usbHandle, cb1.cbInfos[i].boxID));
Console.WriteLine("frmUserForm::CreateTabs - Making new tab with cb.cbInfos[i].usbHandle:" + cb1.cbInfos[i].usbHandle + " and cb.cbInfos[i].boxID:" + cb1.cbInfos[i].boxID);
InitializeControls(controls[i]);
tabCtrlMain.Size = controls[i].Size;
tabCtrlMain.Width += 20;
tabCtrlMain.Height += 100;
this.Width = tabCtrlMain.Width + 20;
this.Height = tabCtrlMain.Height + 50;
newPage.Controls.Add(controls[i]);
tabCtrlMain.TabPages.Add(newPage);
} //no err for this cb
} //controls not null
} //for all cbInfo's
}//if no err in general finding out how many cb's
this.ResumeLayout();
}
由于我对 GetPC 的调用位于子类而不是父类中,因此必须正确创建它。所以我不确定为什么它没有登陆正确的 ValidateDynData 方法。也许我需要以某种方式将我的对象投射到 programDll 中。当我运行程序并检查 _objectInstance 时,它可能是一个问题:变量..................... ................价值
基础:{GenericCrWriter.GenericPC} ......CrWriter.PC
基础安装:................................................ GenericCrWriter.GenericPC
但是,_assembly 指的是 Ko/PC 而不是 Generic/GenericPC。
另外,我的 _assembly.GetType 看起来不错。我的 Generic/parent 没有任何名为 CrWriter.PC 的东西
对于某些子类案例,我正在尝试使用子类方法而不是父类。出于某种原因,我得到了父类方法,但它永远不会得到子类的覆盖。任何想法为什么?我一直在指从父类调用子类方法, 但它没有到达子类的方法。
在我的子类(Ko)的 PC.cs 中:
**注 3/8/2013:PC.cs 在 Ko Visual Studio 项目中。**这包含一个显示的表单 **注 3/7/2013:这是一个以孩子命名的单独的 Visual Studio 项目,我们称之为 Ko。这里重要的类是 PC.cs。除了将数据传递给父级,提供自定义文本框及其名称,验证稍后在父级表单中输入的数据之外,它并没有做太多事情。大部分流程都在父级中,否则。我正在添加 GetPC、setProgramName、setDTF 方法。
public partial class PC : GenericPC
{
String childDllName = ""; //I just added this recently but it doesn't seem useful
GenericPC baseInst = new GenericPC();
public Control GetPC(USB_Comm.CB cbInst, string dllSel, TemplateHApp.Templates.TEMPL[] templ, string dll, SC.SC.SITE c0, int slaveIndex, int BoxID)
{
childDllName = dll;
//call parent class methods
setProgramName();
setDTF();
ProcessDynData();
return baseInst.GetPC(cbInst, dllSel, templ, dll, cert0, slaveIndex, BoxID);
}
public void setProgramName()
{
Console.WriteLine("Ko did stuff");
//Update label on form
var f = new F(); //F is a class in child class containing more info on it
string temp = f.GetProgramName();
baseInst.setProgramName(temp); //this is displayed on parent's form
}
public void setDTF()
{
var f = new F();
string temp = f.DTF();
baseInst.setDTF(temp); //this is displayed on parent's form
}
private void ProcessDynamicData()
{
Console.WriteLine("Ko PC::ProcessDynamicData");
Label lbl_dynData0 = new Label();
Label lbl_dynData1 = new Label();
lbl_dynData0.Text = "AT .";
lbl_dynData1.Text = "VL .";
lbl_dynData0.Location = new Point(57, 25);
lbl_dynData1.Location = new Point(57, 45);
Label[] lbl_dynData_Arr = new Label[4];
lbl_dynData_Arr[0] = lbl_dynData0;
lbl_dynData_Arr[1] = lbl_dynData1;
TextBox tb_dynData0 = new TextBox();
TextBox tb_dynData1 = new TextBox();
tb_dynData0.Location = new Point(67, 25);
tb_dynData1.Location = new Point(67, 45);
tb_dynData0.Size = new Size(151,22);
tb_dynData1.Size = new Size(151, 22);
TextBox[] tb_dynData_Array = new TextBox[4];
tb_dynData_Array[0] = tb_dynData0;
tb_dynData_Array[1] = tb_dynData1;
PC pc = this; //Tried adding this to get past problem but it's not turned out useful
//I think the way I access parent class from child is the problem of why flow in
//parent class isn't reaching overridden method in child when called:
baseInst.addDynamicDataTextBoxes(tb_dynData_Array, lbl_dynData_Arr, childDllName, pc);
}
public override void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{ //I added more info here, but it's probably too much info 3/7/2013
Console.WriteLine("Ko PC::ValidateDynData");
result = -610;
//AT
if ((Convert.ToInt16(tb_dynData_Array[0].Text) >= 1) && (Convert.ToInt16(tb_dynData_Array[0].Text) <= 99))
result = 0;
//VL
if (result == 0)
if ((Convert.ToInt16(tb_dynData_Array[1].Text) >= 69) && (Convert.ToInt16(tb_dynData_Array[1].Text) <= 100))
result = 0;
else
result = -610;
}
在我的父类的 GenericPC.cs 中:
**注 3/8/2013:GenericPC 在 Generic Visual Studio 项目中。**注意 3/7/2013 当子类调用父类初始化重要数据时,父类显示它的表单和字段(我认为简历布局显示它)。接下来,我们在表单上输入数据,包括 Ko 的自定义数据,然后我们点击表单上的一个按钮 (btn_Lock_Config_Click),它需要处理和验证它的数据。我添加了更多方法来获得流动的感觉。父级中的方法比子级多得多(未显示),包括 try/catch 等。
//base
public partial class GenericPC : UserControl
{
//class variables (wave your hands..too much info)
public Control GetPC(USB_Comm.CB cbInstance, string dllSelected, TemplateHApp.Templates.TEMPL[] template, string dll, SC.SC.SITE c0, int slaveIndex, int boxID)
{
cb = cbInstance;
SlaveIndex = slaveIndex;
createControls();
itsDll = dll;
templateArr = template;
return this; //return the control for the control array
}
//called from child class
public void setProgramName(string name)
{
Console.WriteLine("Generic setProgramName slaveIndex:" + SlaveIndex);
lbl_Program_Name.Text = name;
}
//called from child class
public void setDTF(string theDTF)
{
Console.WriteLine("Generic setDTF slaveIndex:" + SlaveIndex);
lbl_Program_Name.Text += " ";
lbl_Program_Name.Text += theDTF;
lbl_Program_Name.Refresh();
}
public void addDynamicDataTextBoxes(TextBox [] tb_dynData, Label [] lblTitle, String childName, Object child)
{
childHasDynamicData = true; //somebody's knocking
itsChildName = childName; //child name isn't turning out to be useful here
itsChild = child; //child isn't turning out to be useful here
Console.WriteLine("Generic addDynamicDataTextBoxes slaveIndex:" + SlaveIndex);
//Display what child wants
for (int i = 0; i < tb_dynData.Length; i++)
{
//assumes calling code knows real estate and planned for it
gb_dynamicData.Controls.Add(lblTitle[i]);
gb_dynamicData.Controls.Add(tb_dynData[i]);
}
itsEnteredDynamicData = tb_dynData; //nothing entered yet
}
private void btn_Lock_Config_Click(object sender, EventArgs e)
{
int status = 1;
Console.WriteLine("Generic btn_Lock slaveIndex:" + SlaveIndex);
//it does some flagging and data checking, etc.
status = processDynamicData();
}
private int processDynData()
{
int returnCode = 0; //I'm setting it to desired value for example
//processes data, puts it into data arrays, etc,
if ((returnCode >= 0) && childHasDynamicData)
ValidateDynData(itsEnteredDynamicData, ref returnCode);
//start here for problem...it never calls child method, as intended
}
public virtual void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Generic::ValidateDynData passing off to child to validate special data");
}
当我在父类中调用 ValidateDynData 时,为什么它不会进入 ValidateDynData 的子类实现?这是我的代码中我试图让子类覆盖父实现的唯一区域,所以我不确定我是否做错了什么?
我检查了在孩子的项目/类中引用了正确版本的 Generic.dll。我在子类中做了一个干净的构建。还有其他应该检查的二进制文件吗?我的反映有问题吗?我对 ValidateDynData 的虚拟/覆盖使用有什么问题吗?
更新:我一直在看代码,我通过创建父/基类的实例进入父类。所以我认为这就是为什么当我在父类中调用它时,我没有进入在子类中被覆盖的 ValidateDynData。有没有另一种方法可以在不创建父级实例的情况下访问父级的方法?
GenericPC baseInst = new GenericPC();
return baseInst.GetPC(cbInst, dllSel, templ, dll, cert0, slaveIndex, BoxID);
** 2013 年 3 月 7 日更新:问题也可能是我按下了父表单上的一个按钮,该按钮启动了一个新线程,通过这样做,它不知道子类,所以这就是为什么 flow 不当我调用 ValidateDynData 方法时,t get to child。