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

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

问题描述

所以我有一个类是一个泛型,它可能需要,在它的方法内,自己创建一个自己的实例与不同种类的通用,这种类型是通过relfection获得。

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

这很重要,因为这个Repository将T映射到一个数据库表(这是我写的ORMish),如果表示T的类有一个代表ANOTHER表的集合能够实例化并将其传递到存储库[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

         PropertyAndAttributes _paa;
         foreach (PropertyInfo Property in type.GetProperties())
 //Let's loop through all the properties.

           {                    
         _paa = new PropertyAndAttributes();
 //Create a new instance each time.

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

        bool MapPropertyAndAttribute = true;
        if (Property.PropertyType.Namespace == "System.Collections.Generic")
    //This is a class we need to map to another table
               {
                   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;
                            }
                        }
                    }
                    if (MapPropertyAndAttribute)
                      PAA.AddPaa(_paa);
            //Add this to the list.
                }
                return PAA;
            }

所以给定
GenericRepository,我会这样做吗?
需要用WORKS替换的行

So given GenericRepository, and I want to make a GenericRepository how would I do this? The line I need to replace with something that WORKS is

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


推荐答案

我想你正在寻找< =http://msdn.microsoft.com/en-us/library/system.type.makegenerictype.aspx> 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天全站免登陆