c# 在运行时创建未知的泛型类型

发布于 2024-12-28 04:33:52 字数 1850 浏览 1 评论 0原文

因此,我有一个泛型类,它可能需要在其自己的方法内使用不同类型的泛型创建自身的实例,其类型是通过反射获得的。

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

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<通过 Property 反射获得的字符串类型> 我该怎么做? 我需要用有效的内容替换的行是:

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

谢谢。

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.

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;
}

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());

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的年少不轻狂 2025-01-04 04:33:52

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

// 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);

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);
天暗了我发光 2025-01-04 04:33:52
Activator.CreateInstance(typeof(GenericRepository<>).MakeGenericType(new Type[] { Property.GetTYpe() }))
Activator.CreateInstance(typeof(GenericRepository<>).MakeGenericType(new Type[] { Property.GetTYpe() }))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文