如何从c#中的类对象中提取字段?

本文关键字:对象 提取 字段 | 更新日期: 2023-09-27 18:16:36

我有一个类,它有一些属性

public class SendData
{
public int MerchantID{get; set;}
public string Name{get; set;}
public int Age{get; set;}
}

点击按钮,初始化用户数据到这个类的对象。

button_click()
{
SendData senddata = new SendData();
senddata.MerchantID = Convert.ToInt32(txtMerchantid.Text);
senddata.Age = Convert.ToInt32(txtAge.Text);
senddata.Name= txtName.Text;
Webservice1.ReceiveDataService serviceObj = new Webservice1.ReceiveDataService();
public bool result = serviceObj.UpdateData(senddata); // calling web service
if(result) //Here is the scenario
lblresult.Text="Updated";
else
lblresult.Text="Operation unsuccessful";
}

现在我怎么能从这个对象上的webservice方法读取所有字段?

我webmethod

:

public bool updatedata()//How to pass that object here
{
 //How can i get those three values in three separate fields in this method like.
 string name =""; //that name from UI;
 int id = ;//Id from UI
 int age= ;//Age from UI 
 //All the field need to be stored in database those coding will come here.
 return true;
}

这很简单,但请帮助我,你也可以建议我一些最好的和替代的方式来实现。谢谢你,

如何从c#中的类对象中提取字段?

您需要有一个类型为SendData的参数,该类型应该存在于web服务应用程序中,并且可以被消费应用程序访问。

public bool updatedata(SendData sendDate)//How to pass that object here
{
    //How can i get those three values in three separate fields in this method like.
    string name =""; //that name from UI;
    int id = ;//Id from UI
    int age= ;//Age from UI 
}

我们应该在web方法之前添加xmlInclude类,以便该类暴露给客户端。

[WebMethod]    
[XmlInclude(typeof(Class_Name))]
public bool updatedata(Class_Name ObjectName)
{
string name =ObjectName.Name;
int id = ObjectName.ID;
int age= ObjectName.Age;
//Here the code for database storage etc., etc., and return the value...
return true;
}
Client has to create the proxy class and pass the object to this web service to do the functionalities.

(Can't send object to SOAP Web Service)

再次感谢大家。