C# Activator.CreateInstance 动态创建类的实例(一)
2024-09-14 15:22:10
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);
}
}
最新文章
- 【HDU】4773 Problem of Apollonius
- WinForm------TreeList加载数据方法
- 深入理解 Win32 PE 文件格式
- LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)
- mysql存储过程笔记
- 排序算法总结(一)插入排序【Insertion Sort】
- linux安装
- approval workflow in sharepoint designer
- Android模拟器常用命令收录
- inline与lnk2001、lnk2019,鸡肋?
- Linux经常使用命令(一) - ls
- Java接口的实例应用:致敬我的偶像——何塞·穆里尼奥
- IOS自带输入法中文不触发KEYUP事件导致vue双向绑定错误问题
- AI 线性回归
- Spring日记_01 之基于maven的Spring环境搭建
- C++编译器详解(一)
- 代码面试集锦 2 - Google
- PHP 如何 安全配置
- conda
- 20155316 2016-2017-2 《Java程序设计》第8周学习总结
热门文章
- 指定等级 Exercise07_01
- Swift开发经验——外部参数名
- 玩转JavaScript正则表达式
- iOS开发笔记_4自定义TabBar
- rundll32.exe的相关使用语句
- oracle 10g函数大全--转换函数
- struts2设置文件上传大小
- How to backup a remote PostgreSQL db and restore it locally?
- C#写的一个视频转换解码器
- 远程管理服务 Windows Remote Management (WS-Management)