[英]C#, Generics, Type and NHibernate
I'm learning the power of generics in C# in conjunction with NHibernate. 我正在与NHibernate一起学习C#中泛型的功能。 I'd like to attempt the following in the pasted code.
我想在粘贴的代码中尝试以下操作。
In an attempt to do some post processing of N number of NHibernate objects I worked on a utility method leveraging generics to make it applicable to all NHibernate mapping classes we use now, or in the future. 为了对N个NHibernate对象进行一些后期处理,我尝试了一种利用泛型的实用程序方法,使其适用于我们现在或将来使用的所有NHibernate映射类。 It works but I need to hard code each call for each mapping class.
它可以工作,但是我需要对每个映射类的每个调用进行硬编码。 This is a pain and will need continuing updating as our schema and mappings change over time.
这是一个痛苦,随着我们的架构和映射会随着时间的推移而不断变化,需要不断进行更新。
I do have an ever up-to-date list of all mapping classes by string name through the NHibernate mappings I generate on the fly. 我确实通过动态生成的NHibernate映射按字符串名称提供了所有映射类的最新列表。 If there was a way to use this list of string names to call my generics based method, I'd be super happy.
如果有一种方法可以使用此字符串名称列表来调用基于泛型的方法,我会非常高兴。
Can anyone tell me if this is possible? 谁能告诉我这是否可能? Do I need to find another route?
我需要寻找其他路线吗?
Thanks so much in advance!!! 提前非常感谢!!!
public static void ProcessSomeItems()
{
// *************************************************************
// As of now I have to list all classes as such to be processed
// It works but I have to update manually when new mapping classes are created
// *************************************************************
NHibDoSomethingUtil<AspnetMembership>();
NHibDoSomethingUtil<AspnetProfile>();
NHibDoSomethingUtil<AspnetRole>();
NHibDoSomethingUtil<AspnetUser>();
// and so forth...
// I have a up-to-date list of all mappings from "HbmMapping" and can get a list of all in the
// list form as below
List<string> mappingNames = new List<string>();
foreach (string mappingName in mappingNames)
{
Type theType = Type.GetType(mappingName);
// I know I'm getting Types and Generics classes and so forth all jumbled but
// how in the heck would I do something like the below?
NHibDoSomethingUtil<theType>(); // Obviously doesn't compile ;-)
}
}
// Generic method
public static void NHibDoSomethingUtil<T>() where T : class
{
using (ISession session = sourceDBSessionFactory.OpenSession())
{
foreach (dynamic item in new List<T>(session.QueryOver<T>().List()))
{
// Process item;
}
}
}
ecsousa gave great input and I was able to accomplish what I needed with something like the following. ecsousa做出了很大的贡献,我能够用下面的东西来完成我所需要的。
foreach (HbmClass mappingClass in mapping.Items)
{
Console.WriteLine(" -- Discovered Mapping: " + mappingClass.Name);
Type mappingClassType = Type.GetType(mappingClass.Name);
var genericMethod = typeof(Migration).GetMethod("NHibDoSomethingUtil");
var method = genericMethod.MakeGenericMethod(mappingClassType);
method.Invoke(null, null);
}
You will need to use Reflection in order to accomplish this. 您将需要使用反射来完成此操作。 Instead of directly calling NHibDoSomethingUtil, try this:
代替直接调用NHibDoSomethingUtil,请尝试以下操作:
var genericMethod = typeof(TheClassName).GetMethod("NHibDoSomethingUtil");
var method = genericMethod.MakeGenericMethod(theType);
method.Invoke(null, null);
Note that you have to replace TheClassName
by the class containing both methods. 请注意,您必须用包含两个方法的类替换
TheClassName
。
Keep in mind the this kind of code is slow, and you should use it very carefully. 请记住,这种代码很慢,您应该非常小心地使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.