views:

99

answers:

3

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

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

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

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

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];
    }

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..?

+4  A: 

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

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.

Jon Skeet
Thanks - that's an answer - i.e. that it's not possible, but do you have any suggestions on how it might be done - is there anything with reflection, etc that can be used to loop through a list of types and create a generic based on them? e.g. passing the types in as an array? I almost felt like giving you a correct mark for teaching me a new word. Now I'll try to drop arity into as many sentences as possible.
Mark D
@Mark: I suspect your best option at the moment is simply to overload the method. (You can overload by arity.) Alternatively, make the method return something other than `bool`, so that you can then call it multiple times in a chained/fluent manner.
Jon Skeet
@Jon - thanks - setting it up as a fluent didn't even occur to me - this is exactly what I'll do! something like NHelper(ConnectionString).From<type1>().From<type2>().Build() - superb!
Mark D
+1  A: 

you seem to repeat the same mistake that newton once did perhaps. He has two cats one was bigger and the other was smaller. He made two holes in door, bigger hole for the bigger one and smaller one for smaller cat. Where as actually he needed one big hole to assist both the cat.

Why don't you create one single method that can deal with as many as types you want..

bool<T[]> MyMethod() where T: Isomething
{
}
this. __curious_geek
Thanks but I'm not sure you read the question correctly. I wasn't creating 3 different methods - just trying to illustrate a point. i.e. the third one isn't valid, but is the kind of thing I need to do.
Mark D
A: 

Actually I just noticed this link - I have to go home now but I might try something like this later if it works:

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

I think if I passed in an array of types and looped through the types with reflection this would work.

Mark D