动态地使用不同的类

时间:2013-08-25 16:06:45

标签: c# list class

我有一个班级:

 public class Test1
 {
     public void assignData(List<CustomClass> customData, string targetFieldName)
     {             
         for(int i=0; i<customData.Count; i++)
         {
             if(customData[i].targetFieldName)
             {
                 customData[i].targetFieldName = newValue;
             }   
         }
     }
 }

 List<customClass1> list1;
 List<customClass2> list2;

customClass1和customClass2完全不同,但它们共享相同的字段“dateAdded”。我希望能够调用Test1.assignData(list1,“dateAdded”)和Test1.assignData(list2,“dateAdded”)。并且list1和list2将更新。我怎样才能做到这一点?谢谢!

3 个答案:

答案 0 :(得分:8)

这样做的最佳方法是拥有一个他们都实现的公共接口,将dateAdded字段公开为属性

interface ICustomClass {
  DateTime dateAdded { get; set; }
}

然后两个类都可以实现该接口,您可以更改函数以使用该接口

public void assignData(IEnumerable<ICustomClass> enumerable) {
  foreach (var customData in enumerable) {
    customData.dateAdded = newValue;
  }
}

修改 在评论中,OP表示他们希望对任何列表进行更新,而不管接口如何。在这种情况下,最好的方法是使用dynamic

public void assignData(IEnumerable<object> enumerable) {
  foreach (dynamic customData in enumerable) {
    try { 
      customData.dateAdded = newValue;
    } catch { 
      // Object doesn't have dateAdded so just move on
    }
  }
}

答案 1 :(得分:1)

如果CustomClass1CustomClass2都来自CustomClass,并且您只想设置targetFieldName的值,那么您需要做的就是将List<T>替换为IEnumerable<T> public void assignData(List<CustomClass> customData, string targetFieldName)

确保公共字段在基类中,以便可以访问它而不必担心派生的实现。

public void assignData(IEnumerable<CustomClass> customData,
                                                string targetFieldName)

covariance

有了这个,您可以因为IEnumerable<object> list = new List<string>(); // This will work List<object> list = new List<string>(); // This won't compile. 而为两个列表调用它。简单的例子 -

{{1}}

答案 2 :(得分:1)

所以我完全赞同@JaredPar,这听起来像你需要一个通用的界面,但它可以用动态。

请注意,如果DateAdded不存在,此示例代码的行为不正确

using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace dynamics_test
{
    class CustomOne
    {
        public string NotInCustomTwo { get; set; }
        public DateTime DateAdded { get; set; }
    }

    class CustomTwo
    {
        public string NotInCustomOne { get; set; }
        public DateTime DateAdded { get; set; }
    }


    [TestFixture]
    public class TestDynamics
    {
        private List<CustomOne> _customOnes;
        private List<CustomTwo> _customTwos;

        [SetUp]
        public void Setup()
        {
            this._customOnes = new List<CustomOne>()
                {
                    new CustomOne {DateAdded = DateTime.Now.AddDays(1), NotInCustomTwo = "some value"},
                    new CustomOne {DateAdded = DateTime.Now, NotInCustomTwo = "some value"}
                };
            this._customTwos = new List<CustomTwo>()
                {
                    new CustomTwo {DateAdded = DateTime.Now.AddDays(1), NotInCustomOne = "some value"},
                    new CustomTwo {DateAdded = DateTime.Now, NotInCustomOne = "some value"}
                };
        }

        [Test]
        public void DynamicsAllowBadThingsMkay()
        {
            var dynamics = _customOnes.Cast<dynamic>().ToList();
            dynamics.AddRange(_customTwos);
            Assert.AreEqual(2, dynamics.Count(d=>d.DateAdded.Date == DateTime.Now.Date));
            foreach (var thing in dynamics)
            {
                Console.WriteLine(thing.DateAdded);
            }
        }
    }
}