Type t=typeof(MyClass);
object[] myobj= t.GetCustomAttributes(typeof (CodeReviewAttribute),false);
foreach (Attribute a in myobj)
{
if (a is CodeReviewAttribute)
{
string abc = ((CodeReviewAttribute)a).Reviewer; //得到类上的自定义属性
}
}
PropertyInfo[] memberInfo = t.GetProperties();
foreach (PropertyInfo var in memberInfo)
{
object[] myabc= var.GetCustomAttributes(false);
foreach (Attribute a in myabc)
{
if (a is CodeReviewAttribute)
{
string abc = ((CodeReviewAttribute)a).Reviewer; //得到属性上的自定义属性
Console.WriteLine(t + "类的属性有" + var.Name + "Reviewer " + abc);
}
}
}
FieldInfo[] myinfo = t.GetFields();
for (int i = 0; i < myinfo.Length; i++)
{
var myatt = myinfo[i].GetCustomAttributes(false);
foreach (Attribute a in myatt)
{
if (a is CodeReviewAttribute)
{
Console.WriteLine(((CodeReviewAttribute)a).Reviewer);
}
}
}
string path = Application.StartupPath + "\\Calculator.dll";
//Assembly assdmbly = Assembly.LoadFile(path);
Assembly assdmbly = Assembly.Load("ClassLibrary1");
Console.WriteLine("得到ClassLibrary1.dll中的所有的类");
Console.WriteLine("***********************************");
foreach (Type var in assdmbly.GetTypes())
{
Console.WriteLine(var.Name + "是ClassLibrary1命名空间下的类");
}
Console.WriteLine("***********************************");
Console.WriteLine("得到ClassLibrary1.dll中的模块集");
Module[] models = assdmbly.GetModules();
foreach (Module var in models)
{
Console.WriteLine(var.Name + "是ClassLibrary1中的一个模块");
}
Console.WriteLine("***********************************");
Console.WriteLine("");
Type type =typeof(ClassLibrary1.Class1); //得到具体的类的类型
Console.WriteLine("具体类型是" + type.Name);
Console.WriteLine("{0}是不是public类型{1}", type, type.IsPublic);
Console.WriteLine("{0}是不是private类型{1}", type, type.IsNotPublic);
Console.WriteLine("***********************************");
Console.WriteLine("");
PropertyInfo[] memberInfo = type.GetProperties();//得到类中的属性
foreach (PropertyInfo var in memberInfo)
{
Console.WriteLine(type+"类的属性有"+var.Name);
Console.WriteLine(type + "类的属性有" + var.Attributes.ToString());
PropertyAttributes sf=var.Attributes;
}
Console.WriteLine("***********************************");
Console.WriteLine("");
Type[] t = type.GetInterfaces();//得到接口
foreach (Type var in t)
{
Console.WriteLine(var.Name + "是ClassLibrary1中的接口");
}
Console.WriteLine("***********************************");
Console.WriteLine("");
Console.WriteLine("方法的返回类型,方法传参的类型");
MemberInfo[] metho = type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
foreach (MemberInfo var in metho)
{
Console.WriteLine("私有的返回类型: 方法名={0} 方法的信息={1}",var.Name,var);
}
MemberInfo[] methopublic = type.GetMethods(BindingFlags.Instance | BindingFlags.Public);
foreach (MemberInfo var in methopublic)
{
Console.WriteLine("公有的返回类型: 方法名={0} 方法的信息={1}", var.Name, var);
}
MemberInfo[] methostatic = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
foreach (MemberInfo var in methostatic)
{
Console.WriteLine("共有的静态返回类型: 方法名={0} 方法的信息={1}", var.Name, var);
}
MemberInfo[] methoprivatestatic = type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic);
foreach (MemberInfo var in methoprivatestatic)
{
Console.WriteLine("私有的静态返回类型: 方法名={0} 方法的信息={1}", var.Name, var);
}
Console.WriteLine("***********************************");
Console.WriteLine("");
Console.WriteLine("这是构造方式的形式");
ConstructorInfo[] con = type.GetConstructors();// 获取构造函数的形式
foreach (ConstructorInfo var in con)
{
Console.WriteLine(var);
}
Console.WriteLine("***********************************");
object obj = Activator.CreateInstance(type, null);//创建一个不带参数的实例
//公有非静态带参数和返回参数的方法的调用
MethodInfo men1 = type.GetMethod("Add");
Console.WriteLine("调用{0}方法",men1);
object[] nums1 = { 5, 4 };//参数
Console.WriteLine(men1.Invoke(obj,nums1));
Console.WriteLine("***********************************");
//私有的非静态方法的调用
MethodInfo men2 = type.GetMethod("Add", BindingFlags.Instance | BindingFlags.NonPublic);
Console.WriteLine(men2.Invoke(obj,null));
Console.WriteLine("***********************************");
//公有的静态带参数的方法的调用
MethodInfo men3 = type.GetMethod("Multiplication", BindingFlags.Public | BindingFlags.Static);
object[] num2 = { 5, 6 };
Console.WriteLine( men3.Invoke(null, num2));
Console.WriteLine("***********************************");
//私有的静态的
MethodInfo men4 = type.GetMethod("Multiplication", BindingFlags.NonPublic | BindingFlags.Static);
Console.WriteLine( men4.Invoke(null,null));
Console.WriteLine("***********************************");
//动态创建一个委托
Console.WriteLine("动态创建一个委托");
TestDelegate dele = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), obj, "Substation");
//Console.WriteLine(dele(9,3));
dele(9, 3);