C#泛型 - 可以创建具有n个泛型类型的方法..? [英] C# generics - possible to create a method with n Generic types..?

查看:176
本文介绍了C#泛型 - 可以创建具有n个泛型类型的方法..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不认为这是可能的,但在这里不用...

I don't think this is possible but here goes...

我想补充的方法可以处理仿制药正NUMER。例如:

I want to add method that can handle n numer of generics. for example :

bool<T> MyMethod() where T: Isomething
{
}

将工作的一种类型。

will work for one type

bool<T,K> MyMethod() where T: Isomething
{
}

将适用于两类

will work for two types

有没有办法用N种工作 - 例如,

Is there a way to work with n types - e.g.

bool<T[]> MyMethod() where T: Isomething
{
}

我想这样做的原因是为了实现静态NHibernate的辅助方法,它可以从多个程序集加载 - 现在它的伟大工程的一个程序集。我现在的方法是,如下所示:

the reason I want to do this is to implement a static nhibernate helper method which can load from multiple assemblies - right now it works great for one assembly. My current method is as shown below:

        public static ISessionFactory GetMySqlSessionFactory<T>(string connectionString, bool BuildSchema)
    {
        //configuring is meant to be costly so just do it once for each db and store statically
        if (!AllFactories.ContainsKey(connectionString))
        {
            var configuration =
            Fluently.Configure()
            .Database(MySQLConfiguration.Standard
                      .ConnectionString(connectionString)
                      .ShowSql() //for development/debug only..
                      .UseOuterJoin()
                      .QuerySubstitutions("true 1, false 0, yes 'Y', no 'N'"))
            .Mappings(m =>
                      {
                          m.FluentMappings.AddFromAssemblyOf<T>();
                          m.AutoMappings.Add(AutoMap.AssemblyOf<T>().Conventions.Add<CascadeAll>);
                      })
            .ExposeConfiguration(cfg =>
                                 {
                                     new SchemaExport(cfg)
                                     .Create(BuildSchema, BuildSchema);
                                 });
            AllFactories[connectionString] = configuration.BuildSessionFactory();
        }

        return AllFactories[connectionString];
    }

当行:m.FluentMappings.AddFromAssemblyOf(),我想补充多种类型,例如

Where the line: m.FluentMappings.AddFromAssemblyOf(), I would like to add multiple types e.g.

foreach(T in T[]){
   m.FluentMappings.AddFromAssemblyOf<T>()

}

显然,这无法工作,我不能完全愚蠢的,但我不是仿制药这么热 - 有人可以证实,这是不可能的:-) ..?什么是您认为实现这一效果的最优雅的方式..?

Obviously this couldn't work I'm not completely dumb but I am not so hot on generics - can someone confirm that this isn't possible :-) ..? What would be the most elegant way of achieving this effect in your opinion..?

推荐答案

没有 - 泛型类型和方法的元数固定在每个类型/方法的基础

No - the arity of generic types and methods is fixed on a per-type/method basis.

这就是为什么有各种不同的动作&LT; ...&GT; Func键&LT; ...&GT; 元组LT; ...&GT; 的框架类型

That's why there are all the different Action<...>, Func<...> and Tuple<...> types in the framework.

有时候这是一个耻辱,但它得到的方式的相对的很少,我怀疑各种各样的事情会多出很多复杂的可变参数数量。

Occasionally that's a shame, but it gets in the way relatively rarely, and I suspect all kinds of things would be a lot more complicated with variable arity.

这篇关于C#泛型 - 可以创建具有n个泛型类型的方法..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆