解决方案TestSolution:3个项目 1:项目(Model)中,Class2继承父类Class1,子类的属性调用了父类的方法 
2:web服务(MyWebService)中,public int MyTest(Class2 class2)  输入参数是子类对象 
3:网站(WebSite)中,构造子类并调用WebService 问题:在调用WebService过程中,传参出现了问题,int类型的值都赋成了0,string值是正确的 
=====================================Model(2个类,Class2继承Class1)===================== 

    public class Class1 
    { 
        private  object[] fieldvalues = null; 
        public object[] FieldValues 
        { 
            get { return fieldvalues; } 
            set { this.fieldvalues = value; } 
        }         public object GetValue(int i) 
        { 
            return FieldValues[i]; 
        } 
        public void SetValue(int i ,object v) 
        { 
            if (fieldvalues == null) 
            { 
                fieldvalues = new object[2]; 
            } 
            FieldValues[i] = v; 
        } 
    } 
} namespace Model 

    public class Class2:Class1 
    { 
        public int Num 
        { 
            get { return Convert.ToInt32(this.GetValue(0)); } 
            set { this.SetValue(0, value); } 
        } 
        public string Word 
        { 
            get { return this.GetValue(1).ToString(); } 
            set { this.SetValue(1, value); } 
        } 
    } 

====================================WebService======================================== 
    [WebMethod] 
    public int MyTest(Class2 class2) 
    { 
        object[] ary = class2.FieldValues; 
        return Convert.ToInt32(ary[0]); 
    } 
======================================WebSite========================================= 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        localhost.Class2 class2 = new Class2(); 
        class2.FieldValues = new object[2] { 10, "thisclass2" }; 
        
        localhost.Service s = new Service(); 
        int a = s.MyTest(class2); 
        string str = s.MyTest2(class2); 
    }得到结果是  a=0  str="thisclass2" 
期望的结果  a=10 str="thisclass2" 在单步调试的过程中,我并没有调用Class2类中的属性,为什么老跑到Num属性的Set方法?这就是为什么a或得的值不是我期望的。而Word的返回类型是string,它的Set的方法却没走。(最后发现,凡是string类型的属性都不走Set方法)是WebService的特性吗? 请强人解决一下,先行感谢了啊

解决方案 »

  1.   

    是啊,我也觉得没有道理的,让我郁闷的有两点
    1:同样的代码,通过WebService调用和普通调用(不用WebService)得到的结果不一样,不通过WebService单步调试都正常,逻辑合理。
    2:为什么通过WebService调用,单步走时,为什么会走Set方法?还只走Int属性的Set方法,而不走String属性的Set方法?个人觉得是WebService有什么规则,但不知道是什么原因,求强人帮忙解决,再次感谢!
      

  2.   

    ======================================class1========================================= 
    using System;
    using System.Collections.Generic;
    using System.Web;/// <summary>
    ///Class1 的摘要说明
    /// </summary>
    public class Class1
    {
        private object[] fieldvalues = null;
        public object[] FieldValues
        {
            get { return fieldvalues; }
            set { this.fieldvalues = value; }
        }
        public object GetValue(int i)
        {
            return FieldValues[i];
        }
        public void SetValue(int i, object v)
        {
            if (fieldvalues == null)
            {
                fieldvalues = new object[2];
            }
            FieldValues[i] = v;
        } 
    }
    ======================================class2========================================= 
    using System;
    using System.Collections.Generic;
    using System.Web;/// <summary>
    ///Class2 的摘要说明
    /// </summary>
    public class Class2:Class1
    {
        public int Num
        {
            get { return Convert.ToInt32(this.GetValue(0)); }
            set { this.SetValue(0, value); }
        }
        public string Word
        {
            get { return this.GetValue(1).ToString(); }
            set { this.SetValue(1, value); }
        }
    }
    ======================================WebService========================================= 
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Services;/// <summary>
    ///WebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class WebService : System.Web.Services.WebService {    public WebService () {        //如果使用设计的组件,请取消注释以下行 
            //InitializeComponent(); 
        }    [WebMethod]
        public int MyTest(Class2 class2)
        {
            object[] ary = class2.FieldValues;
            return Convert.ToInt32(ary[0]);
        }
        [WebMethod]
        public string MyTest2(Class2 class2)
        {
            object[] ary = class2.FieldValues;
            return ary[1].ToString();
        }     
    }
    ======================================WebSite=========================================
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Class2 class2 = new Class2();
            class2.FieldValues = new object[2] { 10, "this is class2" };
            WebService s = new WebService();
            int a=s.MyTest(class2);
            string str=s.MyTest2(class2);
            Response.Write(a.ToString());
            Response.Write("<br/>" + str);
        }