通过类型泛型方法(嵌套通用) [英] Pass type to generic method (nested generic)

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

问题描述

如何我调用下面的方法,而我还没有 TRootEntity ,但只是其键入

>

最终目标是下面的代码运行

  VAR映射器=新ModelMapper() ; 
mapper.Class< MyClass的>(CA =>
{
ca.Id(X => x.Id,地图=>
{
图.Column(MyClassId);
map.Generator(Generators.HighLow,GMAP => gmap.Params(新{max_low = 100}));
});
约物业(X => x.Something,地图= GT; map.Le​​ngth(150));
});



它是用来创建动态NHibernate的 HBM 。更多信息可这里



由于相关的问题看的这里和的这里


解决方案

您不能编写泛型方法传递一个运行时类型来运行。



泛型需要在编译时的类型。



您可能需要使用反射(见先生的答案。费雷拉,关于如何做到这一点点)。


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天全站免登陆