C#反射特性(一)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ReflectionExam
{
class Pepper
{
public string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string color;
public string Color
{
get { return color; }
set { color = value; }
}
public Pepper(string strName, string col)
{
this.name = strName;
this.color = col;
}
public void PrintDatas()
{
Console.WriteLine("Name:" + this.name + "\tColor:" + this.color);
Console.WriteLine("Invoke the PrintDatas method sucessfully");
}
private string GetMessage()
{
return name + color;
}
public static void ShowStaticMessage()
{
Console.WriteLine("ShowStaticMessage");
}
public int Add(int n, int m)
{
return n + m;
}
public void ShowMsg(string str,string msg)
{
Console.WriteLine(str+msg);
}
}
class Program
{
public static void DisplayString(params string[] strNames)
{
foreach (var item in strNames)
{
Console.WriteLine(item.ToString());
}
}
public static void ParamsTest( string[] names)
{
for (int i = 0; i < names.Length;i++ )
{
Console.WriteLine(names[i]);
}
}
delegate void MyMessage(string msg);
event MyMessage mymsg;
delegate void ShowMessage(string name, string color);
public void Show(string msg)
{
Console.WriteLine(msg);
}
static void Main(string[] args)
{
// Program p3 = new Program();
// p3.mymsg += new MyMessage(p3.Show);
// p3.mymsg("www.mronginfo.com");
// //获得类型
// Type typePepper = Type.GetType("ReflectionExam.Pepper");
// //构造参数设置
// object[] conParams = new object[] { "小辣椒", "白色" };
// object pepperObject = Activator.CreateInstance(typePepper, conParams);
// MethodInfo method = typePepper.GetMethod("PrintDatas");
//// method.Invoke(pepperObject, null); ;
// MethodInfo[] methods = typePepper.GetMethods();//获取Pepper类的所用方法,不管是公用的还是私有的,也不管是静态的还是其它继承的,都能够获取。
// foreach (MethodInfo item in methods)
// {
// Console.WriteLine(item.Name);
// }
// //MethodInfo method;
// //PropertyInfo property;
// //ConstructorInfo constructinfo;
// //EventInfo eventinfo;
// //ParameterInfo parameterinfo;
// System.Reflection.Assembly[]assemblys=AppDomain.CurrentDomain.GetAssemblies();
//foreach (var item in assemblys)
//{
// Console.Write(item.GetName());
//}
// Console.WriteLine("**********应用程序集************");
// //Assembly assembly=Assembly.Load("XXX.dll")//加载应用程序集
// Assembly asm = Assembly.GetExecutingAssembly();
// Console.WriteLine("当前程序集的根目录" + asm.CodeBase);//E:\silkworm\ReflectionExam\bin\Debug\ReflectionExam.exe
// Console.WriteLine("当前程序集的入口点" + asm.EntryPoint);//void Main(string[]args);
// AssemblyName asmName=asm.GetName();//这与asm.FullName功能一样
// Console.WriteLine("获取程序集的显示名称:" + asmName.Name);//ReflectionExam
// Console.WriteLine("获取程序集的版本名称:"+asmName.Version);//1.0.0.0
// Console.WriteLine("**********Type类型************");
// Type tPepper = Type.GetType("ReflectionExam.Pepper");
// Console.WriteLine("类型所在的命名空间:"+tPepper.Namespace.ToString());
// Console.WriteLine("获取方法的基本信息" + tPepper.GetMember("PrintDatas"));
// Console.WriteLine("获取属性的基本信息" + tPepper.GetProperty("Name"));
// //获取Pepper类型中的方法基本信息
// //MethodInfo[] methodsSecond = tPepper.GetMethods(BindingFlags.Public);//为什么加上BindingFlags.Public就没有方法了呢????,疑问,BindingFlag.Public干什么用的呢
// MethodInfo[] methodsSecond = tPepper.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance);//原来调用该函数如果为了获取返回值,就必须添加BindingFlag.Instance或者BindingFlag.Static,加上BindingFlag是为了筛选
// foreach (MethodInfo item in methodsSecond)
// {
// Console.WriteLine(item.Name);//输出GetMessage
// }
// //获取方法参数基本信息的使用
// //首先获取参数所在的方法;
// MethodInfo methodinfo = tPepper.GetMethod("Add");
// ParameterInfo[] parameters = methodinfo.GetParameters();
// Console.WriteLine("函数" + methodinfo.Name + "的基本信息");
// foreach (ParameterInfo item in parameters)
// {
// Console.WriteLine("参数名:" + item.Name + "参数类型:" + item.ParameterType);
// }
// //方法的调用
// Pepper pTest = new Pepper("小辣椒测试", "咖啡色");
// Type tpTest = pTest.GetType();
// //或者用静态方法Type tpTest=Type.GetType("ReflectionExam.Pepper");
// MethodInfo tpMethod = tpTest.GetMethod("Add");
// object[] parame = new object[] { 10,230 };
//// object[] paramers = new object[] { "Mronginfo","\tWelcome to you" };
// //int sum = (int)tpMethod.Invoke(tpTest, paramers);
// int sum=(int)tpMethod.Invoke(pTest, parame);//输出Mronginfo Welcome to you
// Console.WriteLine("Sum:" + sum);//输出240
// //object[]pas=new object[]{10,20};
// //MethodInfo tpMethodpt=typePepper.GetMethod("Add");
// //int sum=(int)tpMethodpt.Invoke(pTest, pas);
// //Console.WriteLine(sum.ToString());
try
{
Type typePepper = Type.GetType("ReflectionExam.Pepper");
object[] conparameters=new object[]{"小辣椒","白色"};
object pepperInstance = Activator.CreateInstance(typePepper, conparameters);
// typePepper.InvokeMember("name", BindingFlags.GetField|BindingFlags.Public , null, pepperObject, null);
// Console.WriteLine("GetName:" + name);
Console.WriteLine( typePepper.GetField("name", BindingFlags.Instance|BindingFlags.Public).ToString());
typePepper.InvokeMember("PrintDatas", BindingFlags.InvokeMethod, null, pepperInstance, null);//注意如果PrintDatas为私有的,将因为找不到该方法,而引发异常.
Console.WriteLine(typePepper.GetField("name", BindingFlags.Public|BindingFlags.Instance).ToString());
System.Reflection.PropertyInfo[] pros = typePepper.GetProperties();//获取Pepper类中的所有的属性
foreach (var item in pros)
{
Console.WriteLine("属性名字:" + item.Name + "属性类型:" + item.PropertyType);
}
ShowMessage show = (ShowMessage)Delegate.CreateDelegate(typeof(ReflectionExam.Program.ShowMessage), pepperInstance, "ShowMsg");
show("ganquanfu", "黑色");
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
//Console.WriteLine("finished");
Console.Read();
}
public static ActivationContext t { get; set; }
}
}