0

除了对象的类型外,我有 7 个方法相同。

这是其中的两个:

1)

public MyResult CreateMyResult(MyResult mi, MyVersion myv)
{
    MyVersionEntity myve = _db.MyVersionEntity.Where(r => r.Id == myv.Id).First();
    MyResultEntity mie = new MyResultEntity();
    myve.MyResultEntityAssoc.Add(mie);
    mie = _updateMyResultEntity(mi, mie);
    mi.Id = mie.Id;
    return mi;
}

2)

public MyIssue CreateMyIssue(MyIssue mi, MyVersion myv)
{
    MyVersionEntity myve = _db.MyVersionEntity.Where(r => r.Id == myv.Id).First();
    MyIssueEntity mie = new MyIssueEntity();
    myve.MyIssueEntityAssoc.Add(mie);
    mie = _updateMyIssueEntity(mi, mie);
    mi.Id = mie.Id;
    return mi;
}

有没有办法合并所有这些?

谢谢!

4

1 回答 1

0

使用泛型即

public T Create<T>(T mi, MyVersion myv)
{
    MyVersionEntity myve = _db.MyVersionEntity.Where(r => r.Id == myv.Id).First();
    Entity<T> mie = new Entity<T>();
    myve.EntityAssoc<T>.Add(mie);
    mie = _updateEntity<T>(mi, mie);
    mi.Id = mie.Id;
    return mi;
}

像这样称呼它之后

 Create<MyResult>(...)
 Create<MyIssue>(...)
于 2012-08-04T01:34:01.267 回答