0

我试图找到重构代码的方法,但不知道如何做到这一点。

例如,我们有几个类

class A {
     string name;
     int year;
}

class B {
      long id;
      string code; 
      DateTime currentTime;
}

class C {
      string lastname;
      DateTime currentDate; 
}

后者我需要返回这些类 List、List、List 的对象列表并将它们转换为 Object[][]。

对于每次转换,我都做同样的事情

private Object[][] converAListToObjectArray(List<A> dataList)
        {
            long countRecords = dataList.Count();
            const int countProperty = 2;
            var arrayRes = new object[countRecords][];

            for (int i = 0; i < countRecords; i++)
            {
                var arrayObjProperty = new object[countProperty];
                arrayObjProperty[0] = dataList[i].Name;
                arrayObjProperty[1] = dataList[i].Year;

                arrayRes[i] = arrayObjProperty;
            }
            return arrayRes;
        }

private Object[][] converBListToObjectArray(List<B> dataList)
        {
            long countRecords = dataList.Count();
            const int countProperty = 3;
            var arrayRes = new object[countRecords][];

            for (int i = 0; i < countRecords; i++)
            {
                var arrayObjProperty = new object[countProperty];
                arrayObjProperty[0] = dataList[i].Id;
                arrayObjProperty[1] = dataList[i].Code;
                arrayObjProperty[2] = dataList[i].CurrentTime; 

                arrayRes[i] = arrayObjProperty;
            }
            return arrayRes;
        }

是否可以使用某种设计模式来分离这种转换?

4

4 回答 4

3

您可以编写一个通用函数,该函数使用反射来获取每个对象的字段名称和值。您需要决定它是适用于公共领域还是私有领域。下面的示例同时获取公共和私有字段:

static object[][] ConvertToObjectArray<T>(IList<T> objects)
{
    var fields = (from fieldInfo in typeof(T).GetFields(
        System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
                  orderby fieldInfo.Name
                  select fieldInfo).ToArray();

    object[][] table = new object[objects.Count][];
    for (int i = 0; i < table.Length; i++)
    {
        table[i] = (from fieldInfo in fields
                    select fieldInfo.GetValue(objects[i])).ToArray();
    }
    return table;
}
于 2013-04-24T20:23:59.770 回答
1

您可以使用一个动作并执行以下操作:-

class Program
{
    // Your new function, (doesn't have to be static; just did it for the demo)
    // If you really really want to return object[][] still,
    // You'll need to pass an index to foo as well
    private static List<IList<object>> convert<T>(IList<T> dataList, Action<IList<object>, T> foo)
    {
        var arrayRes = new List<IList<object>>();

        foreach (var item in dataList)
        {
            var arrayObjProperty = new List<object>();
            foo(arrayObjProperty, item);

            arrayRes.Add(arrayObjProperty);
        }
        return arrayRes;
    }

    // The rest is just calling the function with two examples
    static void Main(string[] args)
    {
        var bar = new List<A>();
        bar.Add(new A() { name = "qux", year = 2013 });

        var objects1 = convert(bar, (a, b) =>
        {
            a.Add(b.name);
            a.Add(b.year);
        });

        var baz = new List<B>();
        baz.Add(new B() { code = "qux", id = 2013 });

        var objects2 = convert(baz, (a, b) =>
        {
            a.Add(b.code);
            a.Add(b.id);
        });
    }
}

您可以将其复制到您的 IDE 中进行播放,看看它是如何工作的。基本上,这使用泛型,然后使用一个操作来允许您在传递给该方法的 lambda 中执行每次不同的唯一部分。

于 2013-04-24T20:22:05.913 回答
0

你的Object[][]结构在我看来有点不寻常。你知道Cast列表有一种方法吗?

您可以执行以下操作:

List<A> foo = new List<A> ();

// initialize foo here
var bar = foo.Cast<Object>().ToArray();

这将为您提供一个字段名称完整的对象数组。如果您绝对需要将字段作为数组元素(为什么要这样做),您可以为每个类添加一个 ToObject 方法:

class A
{
    public string name;
    public int year;
    public Object[] ToObject()
    {
        return new Object[] {name, year};
    }
}


    List<A> foo = new List<A>
    {
        new A{name="reacher",year=2013},
        new A{name="Ray",year=2013}
    }; 
    var bux = foo.Select(a => a.ToObject()).ToArray() ;
于 2013-04-24T20:28:21.693 回答
0

您可以简化并使用这样的东西 - 例如对于B...

List<B> list = new List<B> 
{
    new B{ id = 1, code = "", currentTime = DateTime.Now},
    new B{ id = 1, code = "", currentTime = DateTime.Now},
    new B{ id = 1, code = "", currentTime = DateTime.Now},
};

var array = list.Select(x => new object[] { x.id, x.code, x.currentTime }).ToArray();
于 2013-04-24T20:18:05.920 回答