C# 对象实例几种方法
//实例方法1
//new
CreateCalss cc1 = new CreateCalss();
//实例方法2
//获取当前程序集
Assembly asse = Assembly.GetExecutingAssembly();
//获取程序集对象Type t查看方法和字段
Type t = asse.GetType("sp." + "CreateCalss");
//创建对象类型
CreateCalss cc2 = (CreateCalss)Activator.CreateInstance(t);
//实例方法3
//加载指定程序集并创建对象实例
CreateCalss cc3 = (CreateCalss)Assembly.Load("sp").CreateInstance("sp.CreateCalss");
//假设需求根据用户的选择创建一个实例 这个时候需要动态创建对象
//比如选择A A a=new A();
//比如选择B B b=new B();
Assembly ass = Assembly.GetExecutingAssembly();
Type tp = ass.GetType("sp."+"CreateCalss2");
object o = Activator.CreateInstance(tp);
Isp isp = o as Isp;
isp.Open();
public class CreateCalss : Isp
{
public int i; public void Open()
{
Console.WriteLine("");
}
} public class CreateCalss2 : Isp
{
public int i; public void Open()
{
Console.WriteLine("");
}
} interface Isp
{
void Open();
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Type type = typeof(ReflectTest);
object reflectTest = Activator.CreateInstance(type); //不带参数且不返回值的方法的调用
MethodInfo methodInfo = type.GetMethod("MethodWithNoParaNoReturn");
methodInfo.Invoke(reflectTest, null); ReflectTest r = (ReflectTest)Activator.CreateInstance(type);
r.MethodWithNoParaNoReturn(); Console.WriteLine(); ////不带参数且有返回值的方法的调用
//methodInfo = type.GetMethod("MethodWithNoPara");
//Console.WriteLine(methodInfo.Invoke(reflectTest, null).ToString()); //Console.WriteLine(); ////带参数且有返回值的方法的调用
//methodInfo = type.GetMethod("Method1", new Type[] { typeof(string) });
//Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "测试" }).ToString()); //Console.WriteLine(); ////带多个参数且有返回值的方法的调用
//methodInfo = type.GetMethod("Method2", new Type[] { typeof(string), typeof(int) });
//Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "测试", 100 }).ToString()); ////Console.WriteLine(); ////methodInfo = type.GetMethod("Method3", new Type[] { typeof(string), typeof(string) });
////string outStr = "";
////Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "测试", outStr }).ToString()); //Console.WriteLine(); ////静态方法的调用
//methodInfo = type.GetMethod("StaticMethod");
//Console.WriteLine(methodInfo.Invoke(null, null).ToString()); Console.ReadKey();
}
} public class ReflectTest
{
public void MethodWithNoParaNoReturn()
{
Console.WriteLine("不带参数且不返回值的方法");
} public string MethodWithNoPara()
{
Console.WriteLine("不带参数且有返回值的方法");
return "MethodWithNoPara";
} public string Method1(string str)
{
Console.WriteLine("带参数且有返回值的方法");
return str;
} public string Method2(string str, int index)
{
Console.WriteLine("带参数且有返回值的方法");
return str + index.ToString();
} public string Method3(string str, out string outStr)
{
outStr = "bbbb";
Console.WriteLine("带参数且有返回值的方法");
return str;
} public static string StaticMethod()
{
Console.WriteLine("静态方法");
return "cccc";
}
}
}
反射:
class clsDemo
{
public void csDemo(string str)
{
MessageBox.Show(str);
}
}
System.Type t = typeof(Person);
var obj = Activator.CreateInstance(t, null);
t.InvokeMember("csDemo", System.Reflection.BindingFlags.InvokeMethod, null, obj, new object[] { "hell world!" });
使用 :dynamic dynamic obj= Activator.CreateInstance(t, null);
obj.csDemo("hell world!");
C# 对象实例几种方法的更多相关文章
- javascript生成对象的三种方法
/** js生成对象的三种方法*/ // 1.通过new Object,然后添加属性 示例如下: var people1 = new Object(); people1.name = 'xiaohai ...
- Java反射 - 1(得到类对象的几种方法,调用方法,得到包下的所有类)
通过反射获得对象的方法 准备工作: 有一个User类如下 package o1; /** * Created by yesiming on 16-11-19. */ public class User ...
- 读取xml文件转成List<T>对象的两种方法(附源码)
读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法, ...
- 取xml文件转成List<T>对象的两种方法
读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最 ...
- (转载)Java中如何遍历Map对象的4种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- 在Delphi中使用C++对象(两种方法,但都要改造C++提供的DLL)
Delphi是市场上最好的RAD工具,但是现在C++占据着主导地位,有时针对一个问题很难找到Delphi或Pascal的解决方案.可是却可能找到了一个相关的C++类.本文描述几种在Delphi代码中使 ...
- ASP.NET Core 释放 IDisposable 对象的四种方法
本文翻译自<Four ways to dispose IDisposables in ASP.NET Core>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! IDispos ...
- WCF生成客户端代理对象的两种方法的解释
最近在封装WCF,有一些很好的实践就记录下来,大家可以放心使用,所有代码都已经调试过.如果有高手可以大家探讨一下. 在WCF中有两种不同的方法可以用于创建客户端服务对象,他们分别为: 1. 代理构造法 ...
- 反射----获取class对象的五种方法
反射Reflection 配合注解使用会格外强大,反射注解,天生一对 类如何加载? 动态语言和静态语言.我知道是什么,不用总结了. 由于反射,Java可以称为准动态语言. 允许通过反射获得类的全部信息 ...
随机推荐
- Java读取txt文件,计算2011年9月份的通话时间
public class test2 { /** * @param args * @throws IOException */ public static void main(String[] arg ...
- 用Redis实现分布式锁 与 实现任务队列(转)
这一次总结和分享用Redis实现分布式锁 与 实现任务队列 这两大强大的功能.先扯点个人观点,之前我看了一篇博文说博客园的文章大部分都是分享代码,博文里强调说分享思路比分享代码更重要(貌似大概是这个意 ...
- C# 拷贝目录
public class DirectoryExtends { /// <summary> /// 拷贝目录 /// </summary> /// <param name ...
- [c#]params可变参数
摘要 在项目中多多少少会用到params这个关键字,来修饰参数,它的作用,让该参数的个数是可变的,并且可变参数必须是方法的最后一个参数.但如何判断到底有没有为该参数传递值,怎么判断? 一个例子 sta ...
- Sql Server作业
参考资料: http://jingyan.baidu.com/article/49ad8bce7287315834d8fab4.html
- 前端入门级之如何从零开始前端(估计要被人鄙视成LOW货了)入门篇
<!------------------------------------------------------基本说明开始----------------------------------- ...
- 【PHP面向对象(OOP)编程入门教程】16.__toString()方法
我们前面说过在类里面声明“__”开始的方法名的方法(PHP给我们提供的),都是在某一时刻不同情况下自动调用执行的方 法,“__toString()”方法也是一样自动被调用的,是在直接输出对象引用时自动 ...
- Android入门之GPS定位详解
一.LocationManager LocationMangager,位置管理器.要想操作定位相关设备,必须先定义个LocationManager. LocationManger locationMa ...
- 【Android面试】Android面试题集锦 (陆续更新)(最新2012-6-18) eoe上看到的
===============eoeAndroid社区推荐:======================= 1.Android开发新浪面试题[开发者必看哦]下载地址 http://www.eoeand ...
- iOS开发——高级篇——传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...