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

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

问题描述

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



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



最终目标是运行以下代码

  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.属性(x => x.Something,map => map.Length(150));
});

它用于创建动态NHibernate HBM 。更多信息此处



相关问题请参阅这里这里

解决方案

您不能通过传递运行时类型代码通用方法运行。 / p>

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



您可能需要使用反射(请参阅mr的答案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天全站免登陆