【问题标题】:Generic Type Parameters With Type Builder使用类型生成器的泛型类型参数
【发布时间】:2014-11-13 09:51:29
【问题描述】:

基本上我有这门课

public class MyBase<TEntity> where TEntity : class
{
  //Some retrieval stuff
}

我需要这样做

public class MyEntity : MyBase<MyModel>
{
  //More Stuff
}

但使用 TypeBuilder

public TypeBuilder CreateTypeBuilder(Type GenericTypeParameter)
{
  Type MyGenericTypedBase = ????;
  TypeBuilder type = module.DefineType("MyClassName", TypeAttributes.Public, MyGenericTypedBase);
  ...
  return type;
}

提前致谢

请问,如果使用类型生成器生成泛型类型参数,问题是如何获取应该继承的类型

【问题讨论】:

  • 你有什么问题?
  • 除非我遗漏了什么,否则不会是Type MyGenericTypedBase = typeof(MyBase&lt;MyModel&gt;)?;
  • 不好意思,问题是如果泛型类型参数是使用类型生成器生成的并且无法使用 typeof(MyBase),如何获取应该继承的类型

标签: c# c#-4.0 c#-3.0 typebuilder


【解决方案1】:

我刚刚发现答案应该是

Type MyGenericTypedBase = typeof(MyBase<>);
MyGenericTypedBase = MyGenericTypedBase.MakeGenericType(GenericTypeParameter);

谢谢大家的回答

【讨论】:

    猜你喜欢
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多