//实例方法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. spring的PathMatchingResourcePatternResolver-通配符的Resource查找器

    PathMatchingResourcePatternResolver是一个通配符的Resource查找器,包括: /WEB-INF/*-context.xml com/mycompany/**/ap ...

  2. 【转】nanosleep的精度与调度算法的关系 来自:bean.blog.chinaunix.net

     nanosleep的精度与调度算法的关系 2011-12-03 13:05:17 分类: LINUX     Heartwork前辈在我前一篇博文多线程条件下的计数器(2)中的回复中提到,   na ...

  3. C# 在字符串指定位置之前插入新的字符串

    http://zhidao.baidu.com/link?url=XbU4souNCiDk9AbdYWMDj6VMO7AxlnIpcEnAy4JgfaZXxlpjVt2cEoL6GPO9B0WytMq ...

  4. 密码学初级教程(五)消息认证码MAC-Message Authentication Code

    密码学家工具箱中的6个重要的工具: 对称密码 公钥密码 单向散列函数 消息认证码 数字签名 伪随机数生成器 MAC能识别出篡改和伪装,也就是既可以确认消息的完整性,也可以进行认证. 消息认证码的输入包 ...

  5. MongoDB—— 读操作 Core MongoDB Operations (CRUD)

    本文主要介绍内容:从MongoDB中请求数据的不同的方法 Note:All of the examples in this document use the mongo shell interface ...

  6. windows server 2003下安装iis6+php

    参照http://www.myhack58.com/Article/sort099/sort0100/2012/35579.htm 这篇文章,即可! 前 面我写了<windows安装PHP5.4 ...

  7. 淘宝(阿里百川)手机客户端开发日记第十篇 阿里百川服务器环境介绍之API文档的快速链接(四)

    个人感觉比较重要的快速链接: http://open.taobao.com/doc/detail.htm?id=102513 http://open.taobao.com/doc/detail.htm ...

  8. JAVA Io 缓冲输入输出流

    java中提供带缓冲的输入输出流.在打开文件进行写入或读取操作时,都会加上缓冲,提高了IO读写性能. 1. BufferedInputStream 缓冲输入流 2. BufferedOutputStr ...

  9. BZOJ3224——Tyvj 1728 普通平衡树

    1.题目大意:数据结构题,是treap,全都是treap比较基本的操作 2.分析:没啥思考的 #include <cstdio> #include <cstdlib> #inc ...

  10. 使用Monitor调试Unity3D Android程序日志输出(非DDMS和ADB)

    使用Monitor调试Unity3D Android程序日志输出(非DDMS和ADB) http://www.cnblogs.com/mrkelly/p/4015245.html 以往调试Androi ...