反射和特性学习
反射
程序在运行时,可以查看其它程序集或其本身的元数据。一个运行的程序查看本身的元数据或者其他程序集的元数据的行为叫做反射。##
MyClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Myclass
{
private int id;
private int age;
public int number;
public string Name { get; set; }
public string Name1 { get; set; }
private string Name2 { get; set; }
public void Text1()
{
}
private void Text2() {
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Reflection;//引入关于Type的命名空间
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//type类得到一个类的类型
//Assembly程序集类
//每一个类对应一个Type对象,这个Type对象存储了这个 类有哪些方法,哪些数据,哪些成员
Myclass my = new Myclass();//一个类中的数据存储在对象中,但是Type对象只存储类的成员
Type type = my.GetType();//通过对象获取这个对象所属类的Type对象
Console.WriteLine(type.Name);//获取类的名字
Console.WriteLine(type.Namespace);//获取类的命名空间
Console.WriteLine(type.Assembly);//获取程序集的信息
Console.WriteLine("-------获取public的字段-----------");
//获取public的字段
FieldInfo[] array = type.GetFields();//只能获取Public 字段
foreach (FieldInfo item in array)
{
Console.WriteLine(item.Name+" ");
}
Console.WriteLine("------获取punlic的属性----------");
//获取punlic的属性
PropertyInfo[] array2 = type.GetProperties();
foreach (PropertyInfo item in array2)
{
Console.WriteLine(item.Name+" ");
}
Console.WriteLine("----获取public的方法---------");
//获取public的方法,会自动给属性创建一个方法
MethodInfo[] array3 = type.GetMethods();
foreach (MethodInfo item in array3)
{
Console.WriteLine(item.Name+" ");
}
Console.ReadKey();
}
}
}
运行截图
Program.cs
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//type类得到一个类的类型
//Assembly程序集类
Myclass my = new Myclass();
Assembly assem= my.GetType().Assembly;//通过类的type 对象获取它所在的程序集Assembly
Console.WriteLine(assem.FullName);
Type[] types = assem.GetTypes();//获取程序集中的类
foreach (var item in types)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
实现截图:
特性
Obsolete特性
举例:
class Program
{
[Obsolete("这个方法过时了, 请用NewMethod代替")]//Obsolate特性表示这个方法被弃用了,还可以使用
static void OldMethod()
{
Console.WriteLine("OldMethod");
}
[Obsolete("这个方法并不能使用",true)]//第二个参数表示方法标记为错误,不能使用
static void NewMethod()
{
Console.WriteLine("NewMethod");
}
static void Main(string[] args)
{
OldMethod();//能输出
NewMethod();//不能输出
Console.ReadKey();
}
}
Conditional特性
举例:
#define isText//定义一个宏
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApp1
{
class Program
{
[Conditional("isText")]//判断是否定义宏,定义了宏才实现此方法,否则不实现
static void Text1()
{
Console.WriteLine("Text1");
}
static void Text2()
{
Console.WriteLine("Text2");
}
static void Main(string[] args)
{
Text1();
Text2();
Text1();
Console.ReadKey();
}
}
}
创建自定义的特性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
/// <summary>
/// 特性类
/// </summary>
/// 1.特性类的后缀以Attribute结尾
/// 2.需要继承自System.Attribute
/// 3.一般情况下声明为sealed 私有的
/// 4.一般情况下,特性类用来表示目标结构的一些状态(定义一些字段和属性,一般不定义方法)
/// [My("这是一个简单的特性类")],一般这样调用,当我们使用特性时,后面的attribute不需要写
[AttributeUsage(AttributeTargets.Class)]//表示特性类的使用目标,表示可以应用到的数据结构有哪些
sealed class MyAttribute:System.Attribute
{
public string Description { get; set; }
public string VersionNumber { get; set; }
public MyAttribute(string des)
{
this.Description = des;
}
}
}