c# 在运行时创建未知的泛型类型 [英] c# Creating an unknown generic type at runtime

查看:32
本文介绍了c# 在运行时创建未知的泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个泛型类,它可能需要在它自己的方法中使用不同类型的泛型创建自己的实例,其类型是通过反射获得的.

So I have a class that is a generic and it may need to, inside a method of its own, create an instance of itself with a different kind of generic, whose type is obtained through reflection.

这很重要,因为此存储库将 T 映射到数据库表 [这是我正在编写的 ORMish] 并且表示 T 的类是否具有表示另一个表的集合我需要能够实例化并将其传递给存储库 [ala Inception].
我提供了方法,以防它更容易看到问题.

This is important because this Repository maps T to a database table [it's an ORMish I am writing] and if the class that represents T has a collection representing ANOTHER table I need to be able to instance that and pass it to the repository [ala Inception].
I'm providing the method in case it makes it easier to see the problem.

private PropertiesAttributesAndRelatedClasses GetPropertyAndAttributesCollection() 
{
  // Returns a List of PropertyAndAttributes

  var type = typeof(T);
  //For type T return an array of PropertyInfo

  PropertiesAttributesAndRelatedClasses PAA = new PropertiesAttributesAndRelatedClasses();
  //Get our container ready

  //Let's loop through all the properties.
  PropertyAndAttributes _paa;
  foreach(PropertyInfo Property in type.GetProperties())
  {
    //Create a new instance each time.
    _paa = new PropertyAndAttributes();

    //Adds the property and generates an internal collection of attributes for it too
    _paa.AddProperty(Property);

    bool MapPropertyAndAttribute = true;
    //This is a class we need to map to another table
    if (Property.PropertyType.Namespace == "System.Collections.Generic")
    {
      PAA.AddRelatedClass(Property);
      //var x = Activator.CreateInstance("GenericRepository", Property.GetType().ToString());
    }
    else 
    {
      foreach(var attr in _paa.Attrs) 
      {
        if (attr is IgnoreProperty)
        {
          //If we find this attribute it is an override and we ignore this property.
          MapPropertyAndAttribute = false;
          break;
        }
      }
    }
    //Add this to the list.
    if (MapPropertyAndAttribute) PAA.AddPaa(_paa);
  }
  return PAA;
}

如此给定GenericRepository,我想制作一个 GenericRepository<通过从属性反射获得的字符串类型> 我该怎么做?我需要用有效的东西替换的行是:

So given GenericRepository<T>, and I want to make a GenericRepository<string type obtained via reflection from the Property> how would I do this? The line I need to replace with something that WORKS is:

//var x = Activator.CreateInstance("GenericRepository", Property.GetType().ToString());

谢谢.

推荐答案

我认为您正在寻找 MakeGenericType 方法:

I think you're looking for the MakeGenericType method:

// Assuming that Property.PropertyType is something like List<T>
Type elementType = Property.PropertyType.GetGenericArguments()[0];
Type repositoryType = typeof(GenericRepository<>).MakeGenericType(elementType);
var repository = Activator.CreateInstance(repositoryType);

这篇关于c# 在运行时创建未知的泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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