【问题标题】:How can i extract fields from a class object in c#如何从 C# 中的类对象中提取字段
【发布时间】:2014-08-06 14:49:26
【问题描述】:

我有一个有一些属性的类

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 方法上读取该对象的所有字段?

我的网络方法:

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# asp.net web-services class


    【解决方案1】:

    您需要有一个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 服务中创建了一个相同类的镜像,但仍然无法完成。它抛出错误请帮助我清楚地编写函数
    • 什么错误?您只需要在 Web Service 端声明它,如果您想要在我认为不需要的两端,那么您可以将它们放在不同的命名空间中以区分它们。
    【解决方案2】:

    我们应该在 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)

    再次感谢大家。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 2015-12-07
      • 1970-01-01
      相关资源
      最近更新 更多