仅使用一种接受不同类型参数的方法 [英] Use only one method that accepts different typed parameters
问题描述
我有3个不同的数据库表,它们具有相同的5个字段,但是它们没有任何外键关系,因为它们实际上并没有保持相同的值,而是等价的;例如:CompanyA表中有productA,而CompanyB中有productB.
I've 3 different database tables that have the same 5 fields but those does not have any foreign key relation as they are not keeping the same value in fact, but the equivalents; like: CompanyA table has productA and CompanyB has productB.
所以我有3个不同的集合,包括3个等价的字段.因此,我想做的就是使用一个具有companyType和ProductName属性的类,并仅使用一种方法将这3个不同的集合强制转换为一个类对象,例如ResultClass.
so I have 3 different collections include 3 fields that are equivalent. So what I'd like to do is to use a single class that has companyType and ProductName properties and use only one method to cast those 3 different collections to one and only class object, say ResultClass.
public class ResultClass
{
public EnumCompanyType CompanyType { get; set; }
public string ProductName { get; set; }
public ICollection<ResultClass> ConvertAnything(ICollection<T> collection)
{
//Cast it to ResultClass
return resultClassCollection;
}
}
这样我就可以这样使用:
So that I can use this like:
ICollection<ProductA> aCollection = GetCompanyAData();
ICollection<ProductB> bCollection = GetCompanyBData();
ConvertAnything(aCollection);
ConvertAnything(bCollection);
我尝试过动态",但实际上不知道原理(都不知道);所以我搞砸了,我认为这不是为了这个东西.
I've tried "dynamic" but actually don't know the principle (neither have the knowledge); so I've messed it up and I think it's not for this stuff.
我尝试创建扩展方法,但是由于扩展没有参数类型(因为它使用的是ICollection),因此无法访问项目的字段(例如属性)
I've tried to create an extension method but since the extension has no type for its parameter (as it is using ICollection), I can't access the fields of the items (eg. properties)
我正在使用LinqToSql,所有数据库表术语等都属于这个概念,仅此而已.
I'm using LinqToSql and all the database table terms etc. belongs to this concept, nothing else.
我想我应该说清楚:我要避免的多个实例(或者,我还是应该考虑)如下所示:
I think I should made myself clear: The multiple instances that I'm trying to avoid (or shouldn't I, still thinking) is like below
public ICollection<ResultClass> ConvertAnythingForA(ICollection<ProductA> collection)
{
foreach(var item in collection)
{
var result = new ResultClass
{
ProductName = item.ProductA,
ProductType = EnumProductType.ProductA
};
resultClassCollection.Add(result);
}
return resultClassCollection;
}
public ICollection<ResultClass> ConvertAnythingForB(ICollection<ProductB> collection)
{
foreach(var item in collection)
{
var result = new ResultClass
{
ProductName = item.ProductB,
ProductType = EnumProductType.ProductB
};
resultClassCollection.Add(result);
}
return resultClassCollection;
}
先谢谢了.
推荐答案
我可能无法完全理解您,但是由于ProductA,ProductB等具有相同的签名,因此您似乎想要
I may not be understanding you completely, but since ProductA, ProductB etc have the same signature it seems like you'd want an interface like
public interface IResultClass
{
int CompanyType { get; set; }
string ProductName { get; set; }
}
并让那些类仅实现接口.您可以使用可能具有各种类型对象的接口集合.如果您需要任何转换方法,它将看起来像
And have those classes just implement the interface. You could work with collections of the interface that could have objects of the various types. If you need a convert anything method, it would look like
public ICollection<IResultClass> ConvertAnything<T>(ICollection<T> collection) where T : IResultClass
{
return collection.Select(x => (IResultClass)x).ToList();
}
在评论之后-我看到您正在获得非通用的ICollection.您尝试过这样的事情吗?
After comments- I see you that you are getting a non generic ICollection. Did you try something like this:
public ICollection<IResultClass> ConvertAnything(ICollection collection)
{
var x = collection.Cast<IResultClass>();
return x.ToList();
}
这篇关于仅使用一种接受不同类型参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!