本帖最后由 SilverNet 于 2011-12-28 20:48:15 编辑

解决方案 »

  1.   


    嗯,我放上完整代码吧。
    public class CommonDataDetailModel
        {
            public DateTime DataTime { get; set; }
            public decimal? AvgValue { get; set; }
            public decimal? MinValue { get; set; }
            public decimal? MaxValue { get; set; }
            public decimal? TotalValue { get; set; }
            public decimal? OverPercent { get; set; }
        }public class HourData : CommonDataDetailModel
        {
            public decimal HourDataId { get; set; }
            public DateTime DataTime { get; set; } }
            public string PolNo { get; set; }
            public decimal? AvgValue { get; set; }
            public decimal? MinValue { get; set; }
            public decimal? MaxValue { get; set; }
            public decimal? TotalValue { get; set; }
            public decimal? OverPercent { get; set; }
        }
        
        public class DataEntityConverter
        {
              public static object Convert(List<Models.CommonDataDetailModel> entities)
              {   return obj;   }
        }    public class Test
        {
              HourData hourDatas = new HourData();
              DataEntityConverter.Convert(hourDatas);  //这里报错
        }
        
    错误是:错误 9 与“EnterpriseTool.DataEntityConverter.Convert(System.Collections.Generic.List<Models.CommonDataDetailModel>)”最匹配的重载方法具有一些无效参数 E:\EnterpriseTool\app\HourData\MinData.cs 12 53 EnterpriseTool
      

  2.   


    最后一段没有复制全public class CommonDataDetailModel
        {
            public DateTime DataTime { get; set; }
            public decimal? AvgValue { get; set; }
            public decimal? MinValue { get; set; }
            public decimal? MaxValue { get; set; }
            public decimal? TotalValue { get; set; }
            public decimal? OverPercent { get; set; }
        }public class HourData : CommonDataDetailModel
        {
            public decimal HourDataId { get; set; }
            public DateTime DataTime { get; set; } }
            public string PolNo { get; set; }
            public decimal? AvgValue { get; set; }
            public decimal? MinValue { get; set; }
            public decimal? MaxValue { get; set; }
            public decimal? TotalValue { get; set; }
            public decimal? OverPercent { get; set; }
        }
        
        public class DataEntityConverter
        {
              public static object Convert(List<Models.CommonDataDetailModel> entities)
              {   return obj;   }
        }    public class Test
        {
              void t()
              {
                   HourData hourDatas = new HourData();
                   DataEntityConverter.Convert(hourDatas);  //这里报错
              }
        }
        
      

  3.   


     public static object Convert(List<Models.CommonDataDetailModel> entities)
              {   return obj;   }
    参数不对的,list<T> 你传的对象...
    public class HourData : CommonDataDetailModel 既然是继承了  HourData  还需要写子类和父类共有的属性吗,那继承干什么..
      

  4.   


    嗯,谢谢指教,改过来了。
    请问一下为什么普通用对象可以这样写,加上List就不行了呢?
    如:using System;
    using System.Collections.Generic;public class MyClass
    {
    public static void RunSnippet()
    {
    单车 bike = new 单车();
    bike.轮子个数 = "100";
    检测类.检测车辆轮子数(bike);
    }
    public class 车
    {
    public string 轮子个数{get; set;}
    }

    public class 单车: 车
    {
    public string 是否可以变速 {get; set;}
    }

    public class 汽车: 车
    {
    public string 自动挡还是手动挡{get;set;}
    }

    public class 检测类
    {
    public static void 检测车辆轮子数(车 obj){
    Console.Write(obj.轮子个数);
    }
    }
    }这样可以。下面的代码报错:public class MyClass
    {
    public static void RunSnippet()
    {
    List<单车> list = new List<单车>();
    检测类.检测车辆轮子数(list);
    }
    public class 车
    {
    public string 轮子个数{get; set;}
    }

    public class 单车: 车
    {
    public string 是否可以变速 {get; set;}
    }

    public class 汽车: 车
    {
    public string 自动挡还是手动挡{get;set;}
    }

    public class 检测类
    {
    public static void 检测车辆轮子数(List<车> objs){
    foreach(车 obj in objs){
    Console.WriteLine(obj.轮子个数);
    }
    }
    }
      

  5.   

    坦白说,这问题真没遇到过,也没特意学过,主要是真没这么用过,说明书上(MSDN)也没有,专业解答给不了,自从C++模板开始,一直到C#的泛型 我都记得LIST<T>没你这么用的,如果你期望用接口的话,那需要用泛型方法,而不是这种形式!泛型方法的使用public void GList<T>(List<T> entities) where T:车
    {
       //DO SOMETHING HERE
    }//调用时public void TestFunction()
    {
        List<单车> list=new List<单车>();
        GList<单车>(list);
    }你可以把T理解为是另一种形式的object类型,是类模板。可以代替任何一种类型
    where后是对T的约束,这里写了一个 车 的约束,代表所传入的参数类型必须是车这个类或者是其子类型
      

  6.   

    public class 检测类
            {
                public static void 检测车辆轮子数<T>(List<T> objs) where T : 车
                {
                    foreach (车 obj in objs)
                    {
                        Console.WriteLine(obj.轮子个数);
                    }
                }        }检测类.检测车辆轮子数<单车>(new List<单车>());
      

  7.   

    List<单车>  和 List<车> 是两个类型,它们没有关系
      

  8.   

    车 bike = new 单车();
    检测类.检测车辆轮子数(bike);这样应该行把
      

  9.   

    单车 bike = new 单车();
    检测类.检测车辆轮子数((车)bike);转换一下参数的类型