简体   繁体   English

调用泛型方法,返回泛型集合而不知道类型是什么? C#

[英]Invoking a generic method that returns a collection of generics without knowing what the type is? C#

I'm just having a play around with writing an ORM-type project in C#. 我只是想在C#中编写一个ORM类型的项目。

Basically, I have a class structure like this: 基本上,我有这样的类结构:

IDBOperation --- DBReadOperation IDBOperation --- DBReadOperation

DBOperationPool DBOperationPool

Basically, a DBReadOperation maps data from the specified database table to a class in my solution, say for example PersonDetails. 基本上,DBReadOperation将指定数据库表中的数据映射到我的解决方案中的类,例如PersonDetails。

The code to do this was taken from an external source (can't quite remember?), and basically returns a generic collection (Collection). 执行此操作的代码取自外部源(不太记得?),并且基本上返回一个泛型集合(Collection)。

My DBOperationPool needs to be able to take any number of DBReadOperation's and return results for each of them. 我的DBOperationPool需要能够获取任意数量的DBReadOperation并返回每个DBReadOperation的结果。 The logic for that I have down.. but can't seem to get the code to work? 我的逻辑已经失败..但似乎无法使代码工作?

The mapping class also is built on generics.. so how can I instantiate the mapping class to be along the lines of.. 映射类也建立在泛型上。所以如何将映射类实例化为...

MappingClass<?> mappingInstance = new MappingClass<?>();
Collection<?> returnedCollection = mappingInstance.MapData(argument);

How do I (using generics, or reflection, or anything) figure out what to put where the question marks are in the above? 我如何(使用泛型,反射或任何东西)找出上面的问号所在的位置? Can it be done? 可以吗? I've had a search around and nothing seems to be related to this exact problem.. 我有一个搜索周围似乎没有任何关于这个确切的问题..

Obviously, the easy route is making the pool include the type I want to map towards.. but the whole point of the pool is that I can throw DBReadOperation's at it for, say, PersonDetails, CompanyDetails, etc, and have the data mapped to where it needs to be and return the results properly for each type. 显然,简单的方法是让池包含我想要映射的类型..但是池的重点是我可以将DBReadOperation放在它上面,例如PersonDetails,CompanyDetails等,并将数据映射到它需要的位置,并为每种类型正确返回结果。 With this route, I can throw any number of DBReadOperations into a pool, as long as they deal with a specific type for each DBOperationPool instance.. but thats not what I want.. 使用这条路径,我可以将任意数量的DBReadOperations放入池中,只要它们处理每个DBOperationPool实例的特定类型..但这不是我想要的...

Does this make sense at all? 这有意义吗?

Regards, 问候,

Simon 西蒙

Make MappingClass implement some interface, IMappingClass, then refer to that everywhere that you don't know what type the generic will hold. 使MappingClass实现一些接口IMappingClass,然后引用那个你不知道泛型将保持什么类型的地方。

public interface IMappingClass {
   public void DoStuff();
}

public class MappingClass<T> : IMappingClass {
   // stuff
}

// ... elsewhere ...

public void DoMappingStuff(IMappingClass map){
   map.DoStuff();
   // do other stuff...
}

// ...

MappingClass<string> myStringMap = new MappingClass<string>();
DoMappingStuff(myStringMap);

MappingClass<int> myIntMap = new MappingClass<int>();
DoMappingStuff(myIntMap);

Can your DBReadOperation class be generic? 您的DBReadOperation类可以是通用的吗? Meaning, DBReadOperation<T> and then you instantiate it with: 含义, DBReadOperation<T>然后用以下实例化它:

var dbReadOperation = new DBReadOperation<PersonDetails>();

The MappingClass would take in a DBReadOperation and return a T. It could be used like: MappingClass将接受DBReadOperation并返回T.它可以像:

var mapper = new MappingClass<DBReadOperation<PersonDetails>>();
Collection<PersonDetails> collection = mapper.MapData(someArgument);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM