//实例方法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. [MongoDB]mapReduce

    摘要 上篇文章介绍了count,group,distinct几个简单的聚合操作,其中,group比较麻烦一点.本篇文章将学习mapReduce的相关内容. 相关文章 [MongoDB]入门操作 [Mo ...

  2. Swing杂记——Swing中引入Android的NinePatch技术,让Swing拥有Android的外观定制能力

    [摘要] 本文诣在展示如何在Swing中引入 NinePatch技术(早期有文章里中文译作九格图,暂且这么叫吧^_^,但此术非传统移动手机上的功能布局——九格图哦). [准备篇] Q:何为 NineP ...

  3. mapreduce 自定义数据类型的简单的应用

    本文以手机流量统计为例: 日志中包含下面字段 现在需要统计手机的上行数据包,下行数据包,上行总流量,下行总流量. 分析:可以以手机号为key 以上4个字段为value传传递数据. 这样则需要自己定义一 ...

  4. nginx同一iP多域名配置方法

    文章转自:http://blog.itblood.com/nginx-same-ip-multi-domain-configuration.html 下面的是我本机的配置: server { list ...

  5. spring缓存Ehcache(入门2)源码解读

    Ehcache缓存: 解读: Ehcache缓存是在继承spring缓存核心类CacheManager的基础上实现的. 常用类: EhCacheCacheManager:继承自CacheManager ...

  6. MSP430G2333下位机乘法运算需要注意的一个问题

    背景: 最近负责为主板管理电源的电源管理模块编写软体,使用的MCU为MSP430G2333.功能上很简单,即通过板子上的硬件拨码设定,或者通过IIC与主板通信,由主板的BIOS决定开机及关机的延时供电 ...

  7. 用SQLite查看编辑android导出的微信聊天记录

    上一篇我们已经能够完成文字版微信聊天记录导出android了,也即复制或剪切MicroMsg.db文件到电脑,以.db格式结尾的文件是数据库文件(database document),需要安装相关数据 ...

  8. C++中的单例模式(转)

    单例模式也称为单件模式.单子模式,可能是使用最广泛的设计模式.其意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享.有很多地方需要这样的功能模块,如系统的日志输出,G ...

  9. ini文件操作

    Config.ini 文件操作 [SYS] sysname=hy company=hyhy tel=2 using System; using System.Collections.Generic; ...

  10. webpack 教程 那些事儿03-webpack两大精华插件,热加载

    本节主要讲述 webpack的两大经典开发调试插件,热插拔内存缓存机制 文章目录 1. html-webpack-plugin插件的使用 2. webpack-dev-middleware 插件登场 ...