标签:
class Program
{
static void Main(string[] args)
{
Assembly assem = Assembly.Load("mscorlib");//加载系统程序集
PrintInfo(assem);//输出程序集相关信息
assem = Assembly.LoadFrom(@"F:\System.Data.SQLite.dll");//或使用LoadFile()方法
PrintInfo(assem);//输出程序集相关信息
assem = Assembly.GetExecutingAssembly();//获取当前执行代码的程序集
PrintInfo(assem);//输出程序集相关信息
Console.Read();
}
//输出程序集相关信息
static void PrintInfo(Assembly assem)
{
Console.WriteLine("程序集全名:" + assem.FullName);
Console.WriteLine("程序集的版本:" + assem.GetName().Version);
Console.WriteLine("程序集初始位置:" + assem.CodeBase);
Console.WriteLine("程序集位置:" + assem.Location);
Console.WriteLine("程序集入口:" + assem.EntryPoint);
Type[] types = assem.GetTypes();//得到该程序集里所有的类型
Console.WriteLine("程序集下包含的类型数:" + types.Length);
//foreach (var item in types)
//{
// Console.WriteLine("类:" + item.Name);//输出类型名
//}
Console.WriteLine("============================\n");
}
}
class Program
{
static void Main(string[] args)
{
Assembly assembly = Assembly.Load("mscorlib");//加载程序集
Module module = assembly.GetModule("CommonLanguageRuntimeLibrary");//得到指定模块
Console.WriteLine("模块名:"+module.Name);
Type[] types = module.FindTypes(Module.FilterTypeName, "Assembly*");
foreach (var item in types)
{
Console.WriteLine("类名:" + item.Name);//输出类型名
}
Console.Read();
}
}
class Myclass
{
private int Id;
public string Name;
public void Method(int i) { }
}
class Program
{
static void Main(string[] args)
{
Type type = typeof(Myclass);
Console.WriteLine("类型名:" + type.Name);
Console.WriteLine("类全名:" + type.FullName);
Console.WriteLine("命名空间名:" + type.Namespace);
Console.WriteLine("程序集名:" + type.Assembly);
Console.WriteLine("模块名:" + type.Module);
Console.WriteLine("基类名:" + type.BaseType);
Console.WriteLine("是否类:" + type.IsClass);
Console.WriteLine("类的公共成员:");
MemberInfo[] memberInfos = type.GetMembers();//得到所有公共成员
foreach (var item in memberInfos)
{
Console.WriteLine("成员类型:" + item.MemberType + "\t成员:" + item);
}
Console.Read();
}
}
class Myclass
{
public Myclass()
{
Console.WriteLine("创建Myclass对象!");
}
public void Method(int i)
{
Console.WriteLine("输出值:" + i);
}
}
class Program
{
static void Main(string[] args)
{
Type t = typeof(Myclass);//得到类型
object o = Activator.CreateInstance(t);//创建类型的实例
Console.WriteLine("已创建Myclass对象:" + o);
MethodInfo method = t.GetMethod("Method");//获得实例的方法
method.Invoke(o, new object[] { 100 });//调用方法
Console.Read();
}
}
class Program
{
[Obsolete("已过时的方法!", true)]//把true改成false就可以编译通过
static void OldMethod() { }
static void Main(string[] args)
{
OldMethod();//调用过时的方法
Console.Read();
}
}
//自定义的Attribute类命名为XXXAttribute
class HelpAttribute : Attribute
{
private String description;
public HelpAttribute(String Descrition_in)
{
this.description = Descrition_in;
}
public String Description
{
get { return description; }
}
}
class Program
{
[Help("自定义特性")]//使用是不需要写“Attribute”后缀
static void Main(string[] args)
{
Console.Read();
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
class HelpAttribute : Attribute
{
private String description;
public HelpAttribute(String Descrition_in)
{
this.description = Descrition_in;
}
public String Description
{
get { return description; }
}
}
class Program
{
[Help("自定义特性")]
static void Main(string[] args)
{
Console.Read();
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
class HelpAttribute : Attribute
{
private string _description;//可选参数
public string _name;//命名参数
public HelpAttribute(string description)
{
Console.WriteLine("HelpAttribute特性被创建!");
this._description = description;
}
public string Description
{
get { return _description; }
}
public string Name
{
get { return _name; }
set//命名参数,必须要有set方法
{
Console.WriteLine("属性:" + value);
_name = value;
}
}
}
class Program
{
[Help("自定义特性", Name = "李志伟")]//同时使用可选参数与命名参数
static void Main(string[] args)
{
Console.Read();
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
class HelpAttribute : Attribute
{
private string _description;//可选参数
private string _name;//命名参数
public HelpAttribute(string description)
{
Console.WriteLine("====HelpAttribute特性被创建!====");
this._description = description;
}
public string Description
{
get { return _description; }
}
public string Name
{
get { return _name; }
set//命名参数,必须要有set方法
{
Console.WriteLine("====属性:" + value + "====");
_name = value;
}
}
}
//使用了自定义特性的测试类
[Help("类上的自定义特性", Name = "lizhiwei")]
class TestClass
{
[Help("方法上的自定义特性", Name = "李志伟1")]
[Help("方法上的自定义特性", Name = "李志伟2")]
public void TestMethod()
{
Console.WriteLine("===========测试方法===========");
}
}
class Program
{
static void Main(string[] args)
{
Type t = typeof(TestClass);
//获取类上的自定义特性
object[] obis = t.GetCustomAttributes(typeof(HelpAttribute), false);
HelpAttribute attribute = obis[0] as HelpAttribute;
Console.WriteLine("\n" + attribute.Description + "--" + attribute.Name + "\n");
//获取方法上的自定义特性
MethodInfo method = t.GetMethod("TestMethod");
object[] methods = method.GetCustomAttributes(typeof(HelpAttribute), false);
foreach (HelpAttribute help in methods)
{
Console.WriteLine("\n" + help.Description + "--" + help.Name + "\n");
}
Console.Read();
}
}
标签:
原文地址:http://www.cnblogs.com/LiZhiW/p/4316383.html