4.0 高级编程 Reflection
来源:互联网 发布:fc2最新域名怎么设置 编辑:程序博客网 时间:2024/06/01 08:46
System.type类
1.属性
许多布尔属性表示这种类型是一个类还是一个枚举等等。这些特性包括IsAbstract,IsClass等等
Type vecType = typeof(Vector);
Console.WriteLine(vecType.IsAbstract);//return false
Console.WriteLine(vecType.IsClass);//return true
2.方法
获取对应数据类型的成员信息,构造函数,属性,方法和事件
Type t = typeof(double);
MethodInfo[] methods = t.GetMethods();
foreach (MethodInfonextmethodin methods)
{
Console.WriteLine(nextmethod);
}
返回方法的俄细节信息。
Assmeble类
把程序集加载到正在运行的进程中。可以使用静态成员Assmeble.Load和Assemble.LoadFrom。前者的参数是程序集名称,运行库会在各个位置上搜索该程序集。后者的参数是完整的路径
WhatsNewAttribute的例子自定义Attribute 类必须继承于System.Attribute 空间。
namespace Wrox.ProCSharp.WhatsNewAttributes
{
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple =true, Inherited = false)]
public classLastModifiedAttribute:Attribute
{
private DateTimedateModified;
private stringchanges;
private stringissues;
public LastModifiedAttribute(stringdateModified,string changes)
{
this.dateModified = DateTime.Parse(dateModified);
this.changes = changes;
}
public DateTimeDateModified
{
get
{
return dateModified;
}
}
public string Changes
{
get
{
return changes;
}
}
public string Issues
{
get
{
return issues;
}
set
{
issues = value;
}
}
}
[AttributeUsage(AttributeTargets.Assembly)]
public classSupportsWhatsNewAttribute :Attribute
{
}
}
using Wrox.ProCSharp.WhatsNewAttributes;
using System.Collections;
[assembly: SupportsWhatsNew]
namespace Wrox.ProCSharp.VectorClass
{
[LastModified("14Feb 2007","IEnumerable interfaceimplemented " +
"So Vector can now be treated as a collection")]
[LastModified("10Feb 2007","IFormattable interfaceimplemented " +
"So Vector now responds to format specifiers N andVE")]
class Vector :IFormattable
{
public double x, y,z;
public Vector(doublex,double y,doublez)
{
this.x = x;
this.y = y;
this.z = z;
}
[LastModified("10Feb 2002",
"Methodadded in order to provide formatting support")]
public stringToString(string format,IFormatProvider formatProvider)
{
if (format != null)
{
return ToString();
}
else
returnnull;
}
[LastModified("14Feb 2007",
"Class created as part of collection support forVector")]
private classVectorEnumerator
{ }
}
}
using Wrox.ProCSharp.WhatsNewAttributes;
namespace Reflection
{
class Program
{
static StringBuilderoutputText =newStringBuilder(1000);
static DateTimebackDateTo =newDateTime(2007,2, 1);
static void Main(string[] args)
{
Assembly theAssembly = Assembly.Load("Wrox.ProCSharp.VectorClass");
Type[] types = theAssembly.GetTypes();
foreach (Type tin types)
{
Console.WriteLine(t.Name);
Attribute[] attri = Attribute.GetCustomAttributes(t, false);
foreach (Attributeiin attri)
{
LastModifiedAttributelastModifiedAttrib = iasLastModifiedAttribute;
Console.WriteLine(lastModifiedAttrib.DateModified.ToString()+" ; " +lastModifiedAttrib.Changes);
}
}
Console.Read();
}
}
}
- 4.0 高级编程 Reflection
- C#编程 Reflection(反射)
- 使用C#进行Reflection编程
- 使用C#进行Reflection编程
- 使用C#进行Reflection编程
- Reflection
- Reflection
- Reflection
- Reflection
- Reflection
- Reflection
- Reflection
- Reflection
- 编程经验:List Sort by Reflection
- 高级编程
- 《基于MFC的OpenGL编程》Part 16 Reflection
- 《基于MFC的OpenGL编程》Part 15 Reflection
- Go编程基础—反射(reflection)反射三定律
- Disruptor 高并发 无锁
- 除了UUID以外随机生成图片名的方法
- Android app启动时间测试
- 23种设计模式之一(创建型模式)Factory模式
- Android 6.0 运行时权限处理完全解析
- 4.0 高级编程 Reflection
- 《编写高质量iOS与OS X代码的52个有效方法》之第三章要点
- IOS各种调试技巧豪华套餐
- Java基础知识——Java集合详解
- JS比较两个数字的大小问题出错的问题
- javascript 小函数 点击图片 在显示区域显示 不跳转
- 逆天!55英寸高色域曲面电视TCL H8800首触4999惊爆价
- 编写一个make
- java 声明静态Map常量的一种简单方式(转)