参考网址:https://cloud.tencent.com/developer/ask/185965

using System;
using System.Collections.Generic;
namespace ConsActivator
{
class ActivatorHelper
{
public static void TestMain(string[] args)
{
var s1 = CreateListFromType(typeof(Foo));
var s2 = CreateListFromType(typeof(int));
var fo = new Foo();
var s3 = CreateListFromType(fo.GetType()); Console.WriteLine(s1.ToString());
Console.WriteLine(s2.ToString());
Console.WriteLine(s3.ToString());
Console.WriteLine(fo.GetType().Name);
} static object CreateListFromType(Type t)
{
// Create an array of the required type
Array values = Array.CreateInstance(t, ); // and fill it with values of the required type
for (int i = ; i < ; i++)
{
values.SetValue(CreateFooFromType(t), i);
} // Create a list of the required type, passing the values to the constructor
Type genericListType = typeof(List<>);
Type concreteListType = genericListType.MakeGenericType(t); object list = Activator.CreateInstance(concreteListType, new object[] { values }); // DO something with list which is now an List<t> filled with 50 ts
return list;
} // Create a value of the required type
static object CreateFooFromType(Type t)
{
return Activator.CreateInstance(t);
}
}
class Foo
{
public Foo()
{
}
}
}

如何使用Activator.CreateInstance创建一个列表<T>,其中T在运行时是未知的?的更多相关文章

  1. android studio学习---怎么创建一个新的module并且再次运行起来(在当前的project里面)

    选择File->new module出现的界面,选择android application选择下一步,就出现了和刚刚一样的流程了,一步步创建完成即可. 我们看到多了个secondAndroid的 ...

  2. C# Activator.CreateInstance 动态创建类的实例(二)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. 用Activator.CreateInstance代替new实现类的实例化(转)

    一直想得到这样一个函数,输入一个类的名称为参数,返回一个相应的类的实例. 这在工厂模式中是非常有用的 这样,可以使程序有更高的扩展性,例如,,下面的例子 如果现在有一个类,专门用来计算交通工具的速度, ...

  4. 用Activator.CreateInstance代替new实现类的实例化

    一直想得到这样一个函数,输入一个类的名称为参数,返回一个相应的类的实例. 这在工厂模式中是非常有用的 这样,可以使程序有更高的扩展性,例如,,下面的例子 如果现在有一个类,专门用来计算交通工具的速度, ...

  5. 利用django创建一个投票网站(四)

    创建你的第一个 Django 项目, 第四部分 这一篇从第三部分(zh)结尾的地方继续讲起.我们将继续编写投票应用,专注于简单的表单处理并且精简我们的代码. 编写一个简单的表单 让我们更新一下在上一个 ...

  6. 如何用 React Native 创建一个iOS APP?(三)

    前两部分,<如何用 React Native 创建一个iOS APP?>,<如何用 React Native 创建一个iOS APP (二)?>中,我们分别讲了用 React ...

  7. 一个先进的App框架:使用Ionic创建一个简单的APP

    原文  http://www.w3cplus.com/mobile/building-simple-app-using-ionic-advanced-html5-mobile-app-framewor ...

  8. Android入门-新手如何成功创建一个Android小应用

    原文:http://android.eoe.cn/topic/summary 第一课程:Building Your First App [本课内容简介]欢迎加入到安卓应用的开发大潮中!这门课程会教授你 ...

  9. [ASP.NET MVC2 系列] ASP.Net MVC教程之《在15分钟内用ASP.Net MVC创建一个电影数据库应用程序》

    [ASP.NET MVC2 系列]      [ASP.NET MVC2 系列] ASP.Net MVC教程之<在15分钟内用ASP.Net MVC创建一个电影数据库应用程序>       ...

随机推荐

  1. Xamarin.Forms移动开发系列3:项目剖析

    摘要 本文主要进行Xamarin.Forms应用程序剖析. 前言 本文介绍Xamarin.Forms应用程序剖析. 由于本系列重点研究对象为Xamarin.Forms,所以对Xamarin.Andro ...

  2. 测开面试 | Python语言常见问题

    1.面向对象的概念? 面向对象编程,简称OOP,是一种程序设计思想 主要包括:类.继承.多态(子类方法覆盖父类方法).实例.属性.方法 2.什么是进程.线程.协程? 进程:独立数据空间,进程间不共享数 ...

  3. ZROI 暑期高端峰会2019 总结

    FBI Warning:以下博客含有 FBI Warning 的都是包含大量人类本质之一的. 前几天听课: Day1 组合计数 Day1 序列数据结构 Day2 线性代数 Day3 图论 Day3 字 ...

  4. CF1163E Magical Permutation(线性基,构造)

    虽然做起来有一点裸……但是就是想不到啊…… 首先令 $d_i=p_i\oplus p_{i-1}$,那么 $d_i$ 都是 $S$ 中的数,$a_i=d_i\oplus d_{i-1}\oplus \ ...

  5. [LeetCode] 891. Sum of Subsequence Widths 子序列宽度之和

    Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the  ...

  6. [LeetCode] 264. Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  7. CSP-S 2019 简要题解

    从这里开始 又考炸了,sad.....明年应该在准备高考了,考完把坑填了好了. 一半题都被卡常,qswl.[我汤姆要报警.jpg] dfs 怎么这么慢呀,sad..... i7 牛逼! 写的比较混乱, ...

  8. 阿里云等云端服务器VPS改造为ROS-Routeros,最大化利用云端硬盘

    因为ROS官方提供的CHR镜像是用16G版本做出来的,所以我们用dd的方法做出来的ROS,空间都是16G. 如果你的云端是40G空间或者更多呢,也只用了16G空间,是不是很浪费. 我今天看到一篇大神写 ...

  9. LeetCode 220. Contains Duplicate III (分桶法)

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  10. Java非侵入式API接口即文档工具apigcc

    一个非侵入的api编译.收集.Rest文档生成工具.工具通过分析代码和注释,获取文档信息,生成RestDoc文档 前言 程序员一直以来都有一个烦恼,只想写代码,不想写文档.代码就表达了我的思想和灵魂. ...