开发者

Pass type to generic method (nested generic)

开发者 https://www.devze.com 2023-03-16 19:51 出处:网络
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 TRoot

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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号