服务器定义了 方法调用类
 public class TestClass
    {
        public bool GetVarForBiz( ref  IVarForFeeItem<IFeeVariable, IFeeItem>  varForFeeItemList )
        {
            return true;
        }
    }服务器端定义了三个接口:
接口1:
 public interface IVarForFeeItem<T, U>
        where T : IFeeVariable
        where U : IFeeItem
    {        T VarList
        {
            get;
            set;
        }
        U FeeItem
        {
            get;
            set;
        }
    }
接口2:
    public interface IFeeVariable
    {        string Name
        {
            get;
            set;
        }
        string Des
        {
            get;
            set;
        }
        string Type
        {
            get;
            set;
        }
        string Value
        {
            get;
            set;
        }    }
接口3:
 public interface IFeeItem
    {        string Name
        {
            get;
            set;
        }
        string FeeCode
        {
            get;
            set;
        }
        string ShortName
        {
            get;
            set;
        }
      
    }
客户端调用:
  static void Main(string[] args)
        {            TestClass tc = new TestClass();         
            TestVarForFeeItem t = new TestVarForFeeItem();          
            IVarForFeeItem<IFeeVariable, IFeeItem> i = (IVarForFeeItem<IFeeVariable, IFeeItem>)t;
            bool b = tc.GetVarForBiz(ref t);         
            Console.ReadLine();
        }这种调用方法失败,请问大家应如何调用呢? 还是需要修改服务器方法的参数呢?
在客户端定义继承自服务器接口的类: public class TestVarForFeeItem : IVarForFeeItem<TestFeeVariable, TestFeeItem>
    {
 
        private  TestFeeVariable  _VarList;
        private TestFeeItem _FeeItem;
 
        public  TestFeeVariable  VarList
        {
            get
            {
                return _VarList;
            }
            set
            {
                _VarList = value;
            }
        }
        public TestFeeItem FeeItem
        {
            get
            {
                return _FeeItem;
            }
            set
            {
                _FeeItem = value;
            }
        }
    }        public class TestFeeItem : IFeeItem
    {
 
        private string _Name;
        private string _FeeCode;
        private string _ShortName;
        private string _IsEnable;
        private string _MustCharge; 
   
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
        public string FeeCode
        {
            get
            {
                return _FeeCode;
            }
            set
            {
                _FeeCode = value;
            }
        }
        public string ShortName
        {
            get
            {
                return _ShortName;
            }
            set
            {
                _ShortName = value;
            }
        }
        public string IsEnable
        {
            get
            {
                return _IsEnable;
            }
            set
            {
                _IsEnable = value;
            }
        }
        public string MustCharge
        {
            get
            {
                return _MustCharge;
            }
            set
            {
                _MustCharge = value;
            }
        }
    
    
    }

解决方案 »

  1.   

    你这里不能使用ref关键字,你用接口的继承类传递参数进去是没问题的,但是要将接口类型反过来变为那个原来类型就要出错,ref只能用在类型不改变的情况下传递,接口本身是不能实例化的,因此传递的都是继承类对象。
      

  2.   

    这么快就有人回复了 :) 谢谢 qldsrx不知客户端这么定义:
     public class TestVarForFeeItem : IVarForFeeItem<TestFeeVariable, TestFeeItem> 行不行我的本意是想GetVarForBiz( ref IVarForFeeItem<IFeeVariable, IFeeItem> varForFeeItemList )方法中参数,不暴露服务器端定义的实体类。就是不把在服务器端定义的Entity型实体直接给客户端使用,所以定义了接口。  因为服务器端程序和客户端程序 是两个程序
      

  3.   

     static void Main(string[] args)
            {            
                TestClass tc = new TestClass();
                TestVarForFeeItem t = new TestVarForFeeItem();
                 IVarForFeeItem<IFeeVariable, IFeeItem> i = (IVarForFeeItem<IFeeVariable, IFeeItem>)t;
                bool b = tc.GetVarForBiz(i);
                Console.ReadLine();
            }这样写编译没问题 ,
    但是执行这一步: IVarForFeeItem<IFeeVariable, IFeeItem> i = (IVarForFeeItem<IFeeVariable, IFeeItem>)t;
    就会报:无法将类型为“TestVarForFeeItem”的对象强制转换为类型“IVarForFeeItem“的错误
      

  4.   

    这里报错的原因是因为你定义的TestVarForFeeItem并未继承IVarForFeeItem<IFeeVariable, IFeeItem>,而是继承了IVarForFeeItem<TestFeeVariable, TestFeeItem>。
      

  5.   

     谢谢哈,这样定义就ok了
     public class TestVarForFeeItem : IVarForFeeItem<IFeeVariable, IFeeItem>
        {
            private IFeeVariable _VarList;
            private IFeeItem _FeeItem;
            public IFeeVariable VarList
            {
                get
                {
                    return _VarList;
                }
                set
                {
                    _VarList = value;
                }
            }
            public IFeeItem FeeItem
            {
                get
                {
                    return _FeeItem;
                }
                set
                {
                    _FeeItem = value;
                }
            }
        }调用的时候 这样调用:
     TestClass tc = new TestClass();
                TestVarForFeeItem t = new TestVarForFeeItem();
                Console.Write(tc.GetVarForBiz((IVarForFeeItem<IFeeVariable, IFeeItem>)t));
                Console.ReadLine();