interface IRun {
void Run();
}
class Person : IRun
{
public void Run()
{
Console.WriteLine("走,去LOL啊!");
}
}
class Car : IRun
{
public void Run()
{
Console.WriteLine("呜...........");
}
}
class Program
{
static void Main(string[] args)
{
IRun e = new Person();
e.Run();
Console.ReadLine();
}
}
static void Main(string[] args)
{
Console.WriteLine("请输入:Car或Person");
string type = Console.ReadLine();
IRun e = null;
if ("Car" == type)
{
e = new Car();
}else if("Person" == type)
{
e = new Person();
}
if(null != e)
e.Run();
Console.ReadLine();
}
static void Main(string[] args)
{
Console.WriteLine("请输入:Car或Person");
string type = Console.ReadLine();
string classPath = String.Format("namespace.{0}", type);
IRun e = Activator.CreateInstance(null, classPath).Unwrap() as IRun;
if(null != e)
e.Run();
Console.ReadLine();
}
private static void ReflectionIRun1(string className)
{
string classPath = String.Format("namespace.{0}", className);
//参数 null ,指出所要创建类型对象位于当前程序集
var handler = Activator.CreateInstance(null, classPath);
IRun e = (IRun)handler.Unwrap();
Console.WriteLine(e.Run());
}
private static void ReflectionIRun2(string className)
{
string classPath = String.Format("namespace.{0}", className);
//typeof(IRun).Assembly 获取 IRun 类型所在的程序集
object obj = typeof(IRun).Assembly.CreateInstance(null, classPath);
IRun e = (IRun)obj;
Console.WriteLine(e.Run());
}
namespace Lib
{
public class Boss
{
private string name = "老大";
public string Name{
get {return name;}
}
public string Talk()
{
return "你们都被开除了......";
}
//老板不会算账,总是多付钱,所以很有自知之明的将Payfor设为private,防止外部人员调用
private int Payfor(int total)
{
return total + 10;
}
}
}
private static void ReflectionBoss1()
{
string classPath ="Lib.Boss";
//"Lib" 参数指明要加载的程序集(即要创建的对象类型在哪个程序集中定义)
var handler = Activator.CreateInstance("Lib", classPath);
Boss b = handler.Unwrap() as Boss;
Console.WriteLine(b.Talk());
}
private static void ReflectionBoss2()
{
string classPath ="Lib.Boss";
//Assembly.Load("Lib") 加载的程序集(即要创建的对象类型在哪个程序集中定义)
var assembly = Assembly.Load("Lib");
Boss b = (Boss)assembly.CreateInstance(classPath);
Console.WriteLine(b.Talk());
}
private static void ReflectionBoss1()
{
string classPath = "Lib.Boss";
//"Lib" 参数指明要加载的程序集(即要创建的对象类型在哪个程序集中定义)
var handler = Activator.CreateInstance("Lib", classPath);
Boss b = handler.Unwrap() as Boss;
//关键代码
FieldInfo f = b.GetType().GetField("name", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance);
f.SetValue(b, "小二");
Console.WriteLine("{0}:{1}", b.Name, b.Talk());
}
private static void ReflectionBoss1()
{
string classPath = "Lib.Boss";
//"Lib" 参数指明要加载的程序集(即要创建的对象类型在哪个程序集中定义)
var handler = Activator.CreateInstance("Lib", classPath);
Boss b = handler.Unwrap() as Boss;
//关键代码
MethodInfo method = b.GetType().GetMethod("Payfor", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance);
object money = method.Invoke(b, new object[] { 10 });
Console.WriteLine("DW039:老大给我报销10元钱车费......");
Console.WriteLine("{0}:.....,算不清了,给你这些吧。",b.Name);
Console.WriteLine("DW039:......");
Console.WriteLine("{0}:{1}", b.Name,money);
Console.WriteLine("DW039:老大你真棒!");
}
private static void ReflectionBoss1()
{
string classPath ="Lib.Boss";
var handler = Activator.CreateInstance("Lib", classPath);
dynamic b = handler.Unwrap();
Console.WriteLine(b.Talk());
}
private static void ReflectionBoss2()
{
string classPath ="Lib.Boss";
var assembly = Assembly.Load("Lib");
dynamic b = assembly.CreateInstance(classPath);
Console.WriteLine(b.Talk());
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有