//实例方法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# 对象实例几种方法的更多相关文章

  1. javascript生成对象的三种方法

    /** js生成对象的三种方法*/ // 1.通过new Object,然后添加属性 示例如下: var people1 = new Object(); people1.name = 'xiaohai ...

  2. Java反射 - 1(得到类对象的几种方法,调用方法,得到包下的所有类)

    通过反射获得对象的方法 准备工作: 有一个User类如下 package o1; /** * Created by yesiming on 16-11-19. */ public class User ...

  3. 读取xml文件转成List<T>对象的两种方法(附源码)

    读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法, ...

  4. 取xml文件转成List<T>对象的两种方法

    读取xml文件转成List<T>对象的两种方法(附源码)   读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最 ...

  5. (转载)Java中如何遍历Map对象的4种方法

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  6. 在Delphi中使用C++对象(两种方法,但都要改造C++提供的DLL)

    Delphi是市场上最好的RAD工具,但是现在C++占据着主导地位,有时针对一个问题很难找到Delphi或Pascal的解决方案.可是却可能找到了一个相关的C++类.本文描述几种在Delphi代码中使 ...

  7. ASP.NET Core 释放 IDisposable 对象的四种方法

    本文翻译自<Four ways to dispose IDisposables in ASP.NET Core>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! IDispos ...

  8. WCF生成客户端代理对象的两种方法的解释

    最近在封装WCF,有一些很好的实践就记录下来,大家可以放心使用,所有代码都已经调试过.如果有高手可以大家探讨一下. 在WCF中有两种不同的方法可以用于创建客户端服务对象,他们分别为: 1. 代理构造法 ...

  9. 反射----获取class对象的五种方法

    反射Reflection 配合注解使用会格外强大,反射注解,天生一对 类如何加载? 动态语言和静态语言.我知道是什么,不用总结了. 由于反射,Java可以称为准动态语言. 允许通过反射获得类的全部信息 ...

随机推荐

  1. 利用SoapUI 测试web service的方法介绍

    1. 简介 SoapUI是用java开发的测试web service的工具. 2. 安装 2.1. 下载地址 http://www.soapui.org/ 2.2. 安装 By downloading ...

  2. hibernate 批量增加 修改 删除

    4.2  Hibernate的批量处理 Hibernate完全以面向对象的方式来操作数据库,当程序里以面向对象的方式操作持久化对象时,将被自动转换为对数据库的操作.例如调用Session的delete ...

  3. android自定义控件(8)-利用onMeasure测量使图片拉伸永不变形,解决屏幕适配问题

    使用ImageView会遇到的问题 在Android应用中,都少不了图片的显示,ImageView,轮播图,ViewPager等等,很多都是来显示图片的,很多时候,我们都希望图片能够在宽度上填充父窗体 ...

  4. [CentOS]安装命令行终端Terminator工具

    摘要 Terminator是一款跨平台的终端工具,使用的是 GPL 许可证,提供了很多高级的功能.它没有 Guake 和 Yakuake 那样光鲜,但绝对是一款重型武器.它提供的功能包括界面分块,将自 ...

  5. linux脚本编程(shell)浅介 (转载)

    linux脚本(shell)编程 啊,昨天上网看到一篇讲 linux/unix shell 的文章,想想自己最后写这东西也是一年前的事了,想想都快忘光了. 还是整理一下,做一次回顾,以后说不定还用得上 ...

  6. java常用类 --- Object

    Object类 Object类是所有Java类的父类,其位于java.lang包中.任何Java对象,如果没有显示定义父类则它默认Object类作为父类. 方法如下: 其中与线程相关的有5个方法: n ...

  7. [译]git log

    git log git log命令用来显示提交的快照. 能列出来你项目的历史, 能过滤和搜索你指定的一些修改. git status能让你检查工作目录和stage区的状态, git log只提供被co ...

  8. 自动去除nil的NSDictionary和NSArray构造方法

    http://www.jianshu.com/p/a1e8d8d579c7 极分享 http://www.finalshares.com/

  9. bzoj1179 [Apio2009]Atm

    Description Input 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口 ...

  10. wordpress不用插件实现Pagenavi页面导航功能

    Pagenavi 是一个很好的功能,现在 WordPress 博客一般都是使用 WP-Pagenavi 插件来实现,但是如果插件一多的话整个wordpress效率就降低了,我们力求用尽量少的插件来实现 ...