Afshar
Afshar

Reputation: 11483

Pass type to generic method (nested generic)

How can I invoke following method while I have not TRootEntity, but have just its TYPE:

public void Class<TRootEntity>(Action<IClassMapper<TRootEntity>> customizeAction) where TRootEntity : class;

final goal is to run following code

var mapper = new ModelMapper();
mapper.Class<MyClass>(ca =>
{
    ca.Id(x => x.Id, map =>
    {
        map.Column("MyClassId");
        map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 }));
    });
    ca.Property(x => x.Something, map => map.Length(150));
});

It is used to create dynamic NHibernate HBM. More info available here

As related question see here and here.

Upvotes: 8

Views: 42632

Answers (2)

Marino Šimić
Marino Šimić

Reputation: 7342

You cannot code Generic methods to run by passing a runtime Type.

Generics need to have the type at compile time.

You may need to use reflection (see answer of mr. Ferreira that point on how to do that).

Upvotes: 16

user203570
user203570

Reputation:

Have a look at this answer from the great Jon Skeet. You should be able to adapt it to your needs.

Upvotes: 14

Related Questions