这是我作为作家在 stackoverflow 上的首次亮相。我叠...
我有 Windows 窗体应用程序。我将 ServiceReference 添加到 .wsdl Web 服务。一切正常,但我在从泛型类初始化字段时遇到问题。
这个类看起来如下:
public partial class setHuntingGroupRequest : object, System.ComponentModel.INotifyPropertyChanged {
private string fwSessionIdField;
private string pbxNameField;
private AlcHuntingGroup huntingGroupField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=0)]
public string fwSessionId {
get {
return this.fwSessionIdField;
}
set {
this.fwSessionIdField = value;
this.RaisePropertyChanged("fwSessionId");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=1)]
public string pbxName {
get {
return this.pbxNameField;
}
set {
this.pbxNameField = value;
this.RaisePropertyChanged("pbxName");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=2)]
public AlcHuntingGroup huntingGroup {
get {
return this.huntingGroupField;
}
set {
this.huntingGroupField = value;
this.RaisePropertyChanged("huntingGroup");
}
}
public partial class AlcHuntingGroup : object, System.ComponentModel.INotifyPropertyChanged {
private string directoryNumberField;
private string directoryNameField;
private AlcHuntingGroupSearchType searchTypeField;
private string[] membersField;
private bool unavailableAuthorizedField;
private bool releaseAfterTimerField;
private string overflowDirectoryNumberField;
private int entityField;
(……)
private void btnDodajnrdohg_Click(object sender, EventArgs e)
{
SoapDemo.ServiceReference1.AlcPbxManagementPortTypeClient soap = new
SoapDemo.ServiceReference1.AlcPbxManagementPortTypeClient();
setHuntingGroupRequest request = new setHuntingGroupRequest();
setHuntingGroupResponse response = new setHuntingGroupResponse();
request.fwSessionId = session_id;
request.pbxName = "demooxemai42";
try
{
response = soap.setHuntingGroup(request);
this.output.Text = response.resultCode.ToString();
}
catch (Exception error)
{
this.output.Text = "Error in request: " + error + "\n";
}
}
在上面的示例中,我插入以请求两个字段。不幸的是,我还必须插入来自 AlcHuntingGroup 类的所有字段(huntingGroup 是对此类的引用)。我试过这个:
request.fwSessionId = session_id;
request.pbxName = "demooxemain2";
request.huntingGroup.directoryName = "Directory Name";
request.huntingGroup.directoryNumber = "1001";
request.huntingGroup.entity = 1;
//etc
Intellisense 正确地看到了这个字段,但是当我开始调试这个代码时,它会在我有的地方返回一个错误 request.huntingGroup.directoryName = "Directory Name";
System.NullReferenceException:未将对象引用设置为对象的实例 SoapDemo.ServiceReference1.setHuntingGroupRequest.huntingGroup.get 返回 null。
如何防止从huntingGroup 获得空值?
request.huntingGroup.directoryName = "Directory Name";
在你的代码这部分
request.huntingGroup
调用属性的getter。您需要先对其进行初始化或通过以下方式改进 getter:
get {
return this.huntingGroupField ?? (this.huntingGroupField = new AlcHuntingGroup());
}
这种方法称为“延迟初始化” -当您第一次尝试访问HuntGroup属性时,huntingGroupField将自动初始化。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句