如何组合具有不同对象作为参数的方法

时间:2017-12-20 12:42:24

标签: c# .net

我有两个看起来非常相似的方法,所以我想制作一个通用方法。但是,参数略有不同。第一种方法需要两个Event对象列表,第二种方法需要两个Entity对象列表。有没有办法结合这些方法,尽管存在差异?我尝试将参数设置为var变量,但这并不起作用。我已经添加了以下方法:

第一种方法:

public bool CompareEvents(List<Event> modelevents, List<Event> patternevents)
    {
        PropertyInfo[] eventproperties = typeof(Event).GetProperties();
        Event foundEvent = new Event();
        bool eventChecked = false;

        if (patternevents.Count == 0)
            return true;
        foreach (Event patternevent in patternevents)
        {
            foreach(Event modelevent in modelevents)
            {
                if (modelevent.Type == patternevent.Type)
                {
                    eventChecked = true;
                    foundEvent = patternevent;
                    for (int i = 2; i < eventproperties.Length; i++)
                    {
                        if (eventproperties[i].GetValue(foundEvent, null) != null && eventproperties[i].GetValue(modelevent, null) != null)
                        {
                            string x = eventproperties[i].GetValue(foundEvent, null).ToString();
                            string y = eventproperties[i].GetValue(modelevent, null).ToString();

                            if (x != y)
                            {
                                return false;
                            }
                        }

                    }
                }
            }

        }
        if (eventChecked == true)
            return true;
        else return false;
    }

第二种方法:

public bool CompareEntities(List<Entity> modelentities, List<Entity> patternentities)
    {
        PropertyInfo[] eventproperties = typeof(Entity).GetProperties();
        Entity foundEntity = new Entity();
        bool entityChecked = false;

        if (patternentities.Count == 0)
            return true;
        foreach (Entity patternentity in patternentities)
        {
            foreach (Entity modelentity in modelentities)
            {
                if (modelentity.Type == patternentity.Type)
                {
                    entityChecked = true;
                    foundEntity = patternentity;
                    for (int i = 2; i < eventproperties.Length; i++)
                    {
                        if (eventproperties[i].GetValue(foundEntity, null) != null && eventproperties[i].GetValue(modelentity, null) != null)
                        {
                            string x = eventproperties[i].GetValue(foundEntity, null).ToString();
                            string y = eventproperties[i].GetValue(modelentity, null).ToString();

                            if (x != y)
                            {
                                return false;
                            }
                        }

                    }
                }
            }

        }
        if (entityChecked == true)
            return true;
        else return false;
    }

事件对象:

public class Event
{
    public int ID { get; set; }
    public string Type { get; set; }
    public string Party { get; set; }
    public string Subject { get; set; }
    public string CommercialGood { get; set; }
    public string ChangeOfOwnership { get; set; }
    public string DirectionSubject { get; set; }
    public string AttributesSubject { get; set; }
    public string ToGroup { get; set; }
    public string Repeat { get; set; }
    public string PaymentRequired { get; set; }

}

实体对象:

public class Entity
{
    public int ID { get; set; }
    public string Type { get; set; }
    public string Role { get; set; }

}

2 个答案:

答案 0 :(得分:0)

编辑完成后,您似乎需要从

开始
interface IAmEventEntity { string Type {get; }}

class Entity : IAmEventEntity  { ... }
class Event : IAmEventEntity  { ... }

然后你可以写

public bool CompareEventsOrEntities<T>(List<T> models, List<T> patterns)
    where T : IAmEventEntity  , new()
{
   T foundItem = new T();
   ...
}

但请考虑使用AutoMapper来抽取这些属性。

答案 1 :(得分:-1)

您正在寻找的是generic methods。这些类型的功能旨在减少不同数据类型的冗余代码原因。

例如

public static bool Compare<T>(T entries)
{
    // ...
    return true;
}

测试上面的功能

var t1 = new[] { "t", "t" };
var t2 = new[] { 1, 2 };

Compare(t1);
Compare(t2);
相关问题