oke*_*eld 1 .net c# reflection
我正在循环一个具有多个属性的类,并且正在搜索具有相同ID的任何文本框.如果有匹配,那么我想将属性值更新为文本框值,但我收到此错误:
对象与目标类型不匹配
这是代码:
foreach (var prop in contactInfo.GetType().GetProperties())
{
var ctrl = WizardCampaign.FindControl(prop.Name) ?? Page.Master.FindControl(prop.Name);
if (ctrl != null)
{
if (ctrl.GetType() == typeof(TextBox))
{
var r = (TextBox)ctrl;
prop.SetValue(prop, r.Text, null);
}
}
}
Run Code Online (Sandbox Code Playgroud)
看这里:
prop.SetValue(prop, r.Text, null);
Run Code Online (Sandbox Code Playgroud)
SetValue应该将您想要更改的对象作为第一个参数,但是您要传递该PropertyInfo对象.我相信你的实际代码应该是:
prop.SetValue(contactInfo, r.Text, null);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1521 次 |
| 最近记录: |