C#泛型 - 可以用n个泛型类型创建一个方法..?

时间:2010-09-15 11:14:18

标签: c# .net nhibernate generics

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

我想添加可以处理泛型的数字的方法。例如:

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

适用于一种类型

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

适用于两种类型

有没有办法处理n种类型 - 例如

bool<T[]> MyMethod() where T: Isomething
{
}
我想这样做的原因是为了实现一个静态的nhibernate辅助方法,它可以从多个程序集加载 - 现在它适用于一个程序集。我目前的方法如下所示:

        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(),我想添加多种类型,例如。

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

}

显然这不起作用我不是完全愚蠢但是我对泛型不太热 - 有人可以确认这是不可能的:-) ..?在你看来,实现这种效果最优雅的方式是什么??

4 个答案:

答案 0 :(得分:6)

不 - 泛型类型和方法的优点是基于每种类型/方法修复的。

这就是为什么框架中存在所有不同的Action<...>Func<...>Tuple<...>类型的原因。

偶尔会感到羞耻,但它很少会妨碍相对,而且我怀疑各种各样的东西会因为变量而变得更加复杂。

答案 1 :(得分:0)

你似乎重复了牛顿曾经做过的同样的错误。他有两只猫,一只较大,另一只较小。他在门上打了两个洞,较大的洞用于较小的洞,较小的洞用于较小的猫洞。实际上,他需要一个大洞来帮助这只猫。

为什么不创建一个可以处理所需类型的方法。

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

答案 2 :(得分:0)

其实我刚刚注意到这个链接 - 我现在必须回家了,但是如果有效的话我可能会尝试这样的事情:

http://geekswithblogs.net/marcel/archive/2007/03/24/109722.aspx

我认为如果我传入一个类型的数组并使用反射循环这些类型,这将起作用。

答案 3 :(得分:0)

正如其他人指定的那样,你不能这样做:

bool MyMethod<T[]>() where T: ISomething

但你可以这样做:

bool MyMethod<T>(params T[] somethings) where T : ISomething

例如:

public interface ISomething { string Name { get; set; } }

public class SomethingA : ISomething { public string Name { get; set; } = nameof(SomethingA); }
public class SomethingB : ISomething { public string Name { get; set; } = nameof(SomethingB); }

void MyMethod<T>(params T[] somethings) where T : ISomething
{
    foreach (var something in somethings)
    {
        if (something != null)
            Console.WriteLine(something);
    }
}

// Use it!
ISomething a = new SomethingA();
ISomething b = new SomethingB();

// You don't need to specify the type in this call since it can determine it itself.
MyMethod(a, b);

// If calling it like this though you do:
MyMethod<ISomething>(new SomethingA(), new SomethingB());

C#交互式窗口输出:

> MyMethod(a, b);
Submission#0+SomethingA
Submission#0+SomethingB
> 
> MyMethod<ISomething>(new SomethingA(), new SomethingB());
Submission#0+SomethingA
Submission#0+SomethingB

因此,您可以接收您想要的类型(符合通用类型)并循环遍历它们并按照您指定的方式调用您的代码。你也可以不使用泛型,只需要参加params对象[]的事情;但如果可以,我建议强烈输入。

如果有人看到这个,请告诉我,如果我离开基地或误解了这个问题......谢谢!