反射機制基礎類
反射機制的功能與介紹
反射層次模型圖
類獲取程序及信息
類獲取程序集模塊信息
類獲取類型的信息
利用反射調用方法
特性(Attribute)
介紹
示例
自定義特性
自定義特性說明
示例
類的使用
可選參數與命名參數
標識符
通過反射獲取Attributes
Main([] args)
assem = .Load();
.LoadFrom();LoadFile()方法
.GetExecutingAssembly();
.Read();
PrintInfo( assem)
.WriteLine(:"
.WriteLine("
.WriteLine(:"
.WriteLine("
.WriteLine("
[] types = assem.GetTypes();
.WriteLine(:"
" + item.Name);//輸出類型名
.WriteLine();
Main([] args)
assembly = .Load();
module = assembly.GetModule();
.WriteLine("
[] types = module.FindTypes(.FilterTypeName, );
( item types)
.WriteLine("
.Read();
Id;
Name;
Method( i) { }
Main([] args)
type = ();
.WriteLine(:"
.WriteLine("
.WriteLine(:"
.WriteLine("
.WriteLine(:"
.WriteLine("
.WriteLine("
.WriteLine("
[] memberInfos = type.GetMembers();
( item memberInfos)
.WriteLine(""
.Read();
Myclass()
.WriteLine(Myclass對象!"
Method( i)
.WriteLine("
Main([] args)
t = ();
o = .CreateInstance(t);
.WriteLine(Myclass對象:"
method = t.GetMethod();
[] { 100 });
.Read();
(Attribute)
(")]true改成false就可以編譯通過
OldMethod() { }
Main([] args)
.Read();
Attribute類命名為XXXAttribute
:
description;
HelpAttribute( Descrition_in)
.description = Descrition_in;
Description
{ description; }
("Attribute”後綴
Main([] args)
.Read();
(.Method, AllowMultiple = , Inherited = )]
:
description;
HelpAttribute( Descrition_in)
.description = Descrition_in;
Description
{ description; }
("
Main([] args)
.Read();
(.Method, AllowMultiple = , Inherited = )]
:
_description;
_name;
HelpAttribute( description)
.WriteLine("
._description = description;
Description
{ _description; }
Name
{ _name; }
,必須要有set方法
.WriteLine(");
;
(""
Main([] args)
.Read();
:
_description;
_name;
HelpAttribute( description)
.WriteLine(===="
._description = description;
Description
{ _description; }
Name
{ _name; }
,必須要有set方法
.WriteLine(" + );
;
(")]
("1"
("2"
TestMethod()
.WriteLine(==========="
Main([] args)
t = ();
[] obis = t.GetCustomAttributes((), );
attribute = obis[0] ;
.WriteLine( + attribute.Description + + attribute.Name + );
method = t.GetMethod();
[] methods = method.GetCustomAttributes((), );
( help methods)
.WriteLine( + help.Description + + help.Name + );
.Read();