C# Activator.CreateInstance 动态创建类的实例(一)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Kernel.SimpleLibrary
{
public class Person
{
private string name; public Person(){ } public Person(string name)
{
this.name = name;
} public string Name
{
get { return this.name; }
set { this.name = value; }
} public override string ToString()
{
return this.name;
}
}
}
using System;
using System.Reflection;
using System.Runtime.Remoting; public class Program
{
static void Main(string[] args)
{
//创建在指定程序集中定义的指定类型的新实例
//assemblyName = 命名空间,typeName = 命名空间.类名
ObjectHandle handle = Activator.CreateInstance("Kernel.SimpleLibrary", "Kernel.SimpleLibrary.Person");
Object p = handle.Unwrap();
Type t = p.GetType();
PropertyInfo prop = t.GetProperty("Name");
if (prop != null)
prop.SetValue(p, "Hello world!"); MethodInfo method = t.GetMethod("ToString");
Object retVal = method.Invoke(p, null);
if (retVal != null)
Console.WriteLine(retVal);
}
}
C# Activator.CreateInstance 动态创建类的实例(一)的更多相关文章
- C# Activator.CreateInstance 动态创建类的实例(二)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C#——反射动态创建类的实例
“反射”其实就是利用程序集的元数据信息. 反射可以有很多方法,编写程序时请先导入 System.Reflection 命名空间. 若要反射当前项目中的类(即当前项目已经引用它了),可以使用下面的写法. ...
- c# 利用反射 从json字符串 动态创建类的实例 并动态为实例成员赋值
转自 http://hi.baidu.com/wjinbd/item/c54d43d998beb33be3108fdd 1 创建自己要用的类 class stu { string _name; int ...
- C# 利用反射根据类名创建类的实例对象
“反射”其实就是利用程序集的元数据信息. 反射可以有很多方法,编写程序时请先导入 System.Reflection 命名空间. 1.假设你要反射一个 DLL 中的类,并且没有引用它(即未知的类型): ...
- 【转】C# 利用反射根据类名创建类的实例对象
原文地址:https://www.cnblogs.com/feiyuhuo/p/5793606.html “反射”其实就是利用程序集的元数据信息. 反射可以有很多方法,编写程序时请先导入 System ...
- OC 反射-->动态创建类
系统方法 NSLog(@"%s", __func__); //打印出类的方法名称,如: //打印结果:2018-02-22 10:52:15.394575+0800 DemoRun ...
- C++反射机制:可变参数模板实现C++反射(使用C++11的新特性--可变模版参数,只根据类的名字(字符串)创建类的实例。在Nebula高性能网络框架中大量应用)
1. 概要 本文描述一个通过C++可变参数模板实现C++反射机制的方法.该方法非常实用,在Nebula高性能网络框架中大量应用,实现了非常强大的动态加载动态创建功能.Nebula框架在码云的仓库地 ...
- Python中type()详解:动态创建类
众所周知: type()函数可以查看变量的类型: 先看一个简单的列子来看一下type查看变量类型 class Animal(): pass a=Animal() print(type(a)) prin ...
- Python 中使用动态创建类属性的机制实现接口之后的依赖
我们在自动化测试中经常会需要关联用例处理,需要动态类属性: 推荐使用第二种方法: 创建:setattr() 获取:getattr() 两种,如何创建 类属性 loan_id # 第一种,创建 # 类名 ...
随机推荐
- POJ 2115 C Looooops(Exgcd)
[题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, ...
- 【分块】bzoj2453 维护队列
http://www.cnblogs.com/autsky-jadek/p/4020296.html 同bzoj2120. #include<cstdio> #include<cma ...
- 【周期性/容斥+二分】POJ2773-HAPPY 2006
[题目大意] 求与n互质的第k个数. [思路] 先求出小于k且与n互质的数,再利用gcd(bt+a,b)=gcd(a,b)的性质求解,效率低.枚举与n互质的数的效率是O(nlogn),求解第k个数的效 ...
- virtualenvwrapper的安装及问题解决
安装virtualenvwrapperyum install python-setuptools python-develpip install virtualenvwrapper # linux下 ...
- IO多路复用 select、poll、epoll
什么是IO多路复用 在同一个线程里面, 通过拨开关的方式,来同时传输多个(socket)I/O流. 在英文中叫I/O multiplexing.这里面的 multiplexing 指的其实是在单个线程 ...
- pythonGUI编程打开默认浏览器
代码: from tkinter import * import webbrowser root = Tk() text = Text(root,width=30,height = 5) text.p ...
- iis日志字段解析
IIS日志字段 #Software: Microsoft Internet Information Services 7.5 #Version: 1.0 #Date: 2013-08-21 01:00 ...
- [HTML/CSS]uploadify自定义按钮样式
概述 在项目中经常用到uploadify上传插件,但是FLASH按钮的外观往往跟我们网页的设计的主题色不太搭配.这时就需要对其样式进行修改. 样式文件是uploadify.css. 打开这个文件后,你 ...
- 修改input type=file 标签默认样式的简单方法
<html><head><title></title></head><body><form id="upload ...
- flask配置选项中的TRAP_HTTP_EXCEPTIONS会阻止自动跳转
参考:http://www.pythondoc.com/flask/config.html Flask 对象的 config 属性. 这是Flask自身放置特定配置的地方,同时也是flask扩展模块放 ...