1
我正在循環一個具有多個屬性的類,並且正在搜索具有相同ID的任何文本框。如果有一個匹配,那麼我想更新的屬性值,該文本框的值,但我收到此錯誤:反射返回對象中的SetValue與目標類型不匹配
Object does not match target type
這是代碼:
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);
}
}
}
而不是'如果(CTRL!= NULL)'和'如果(ctrl.GetType()== typeof運算(文本框))'你可以將它們結合起來,包括派生的對象從文本框使用'if(ctrl是TextBox)' – Silvermind