代码之家  ›  专栏  ›  技术社区  ›  si2030

dotnet Core-需要创建一个泛型扩展方法,该方法有两个泛型,一个是扩展

  •  0
  • si2030  · 技术社区  · 5 年前

    我试图创建一个通用的CSV转换静态方法。它有实体-扩展名。。设为“T”,但我也需要根据实体包含不同的上下文。有两个数据库,每个都有一个郊区实体和DBs上下文。

    这就是我的出发点:

        public static Suburb FromCsv(string csvLine, CATALOGContext context)
        {
            if (csvLine == null) throw new ArgumentNullException(nameof(csvLine));
            if (context == null) throw new ArgumentNullException(nameof(context));
    
            var values = csvLine.Split(',');
            if (context.States == null) return null;
            if (values.Length <= 3) return null;
            var suburb = new Suburb
            {
                PostCode = values[0],
                SuburbName = values[1],
                State = context.States.FirstOrDefault(s => s.StateShortName == values[2]),
                Latitude = Convert.ToDouble(values[3], CultureInfo.CurrentCulture),
                Longitude = Convert.ToDouble(values[4], CultureInfo.CurrentCulture)
            };
    
            return suburb;
        }
    

    这就是我所拥有的,但显然不对。

            public static T FromCsv<T, I>(this T source, string csvLine, I context)
            {
                if (csvLine == null) throw new ArgumentNullException(nameof(csvLine));
                if (context == null) throw new ArgumentNullException(nameof(context));
    
                var values = csvLine.Split(',');
                if (context.States == null) return null;
                if (values.Length <= 3) return null;
                var suburb = new Suburb
                {
                    PostCode = values[0],
                    SuburbName = values[1],
                    State = context.States.FirstOrDefault(s => s.StateShortName == values[2]),
                    Latitude = Convert.ToDouble(values[3], CultureInfo.CurrentCulture),
                    Longitude = Convert.ToDouble(values[4], CultureInfo.CurrentCulture)
                };
    
                return suburb;
            }
    

    它不知道这是一个dbContext(context),所以我需要使用where子句,我不确定如何做到这一点。此外,每个dbContext都有一个“State”,但由于上述原因这是错误的-不知道它的上下文。

    0 回复  |  直到 5 年前
        1
  •  2
  •   Asherguru    5 年前

    你不能用特定的实体来返回T。你必须用T来返回T。为什么不用这个简单的实体呢?

    public enum DBcon
    {
          DB1, DB2
    }
    public static Suburb FromCsv(string csvLine, DBcon con)
    {
        var context = con == DBcon.DB1 ? new CATALOGContext() : new DB2Context();
    
        if (csvLine == null) throw new ArgumentNullException(nameof(csvLine));
        if (context == null) throw new ArgumentNullException(nameof(context));
    
        var values = csvLine.Split(',');
        if (context.States == null) return null;
        if (values.Length <= 3) return null;
        var suburb = new Suburb
        {
            PostCode = values[0],
            SuburbName = values[1],
            State = context.States.FirstOrDefault(s => s.StateShortName == values[2]),
            Latitude = Convert.ToDouble(values[3], CultureInfo.CurrentCulture),
            Longitude = Convert.ToDouble(values[4], CultureInfo.CurrentCulture)
        };
    
        return suburb;
    }