将类型传递给泛型方法(嵌套泛型) [英] Pass type to generic method (nested generic)

查看:50
本文介绍了将类型传递给泛型方法(嵌套泛型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我没有 TRootEntity,但只有它的 TYPE 时,如何调用以下方法:

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

最终目标是运行以下代码

var mapper = new ModelMapper();mapper.Class(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));});

用于创建动态NHibernate HBM.更多信息可用这里>

作为相关问题,请参阅 这里这里.

解决方案

您不能通过传递运行时类型来编写要运行的泛型方法.

泛型需要在编译时具有类型.

您可能需要使用反射(请参阅 Ferreira 先生关于如何做到这一点的回答).

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

这篇关于将类型传递给泛型方法(嵌套泛型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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