争怎路由网:是一个主要分享无线路由器安装设置经验的网站,汇总WiFi常见问题的解决方法。

在.NET运行时知道分类信息(1) Paul_Ni(原作)

时间:2024/7/16作者:未知来源:争怎路由网人气:

BindingFlags.Instance);
Console.WriteLine ("//Constructors");
PrintMembers (ci);
 }
 public static void PrintMembers(MemberInfo [] ms) {
foreach (MemberInfo m in ms) {
Console.WriteLine ("{0}{1}", " ", m);
}
Console.WriteLine();
 }
}

MemberInfo、MethodInfo、FieldInfo 和 PropertyInfo



使用 MemberInfo、MethodInfo、FieldInfo 或 PropertyInfo 对象可获取有关类型的方法、属性、事件、字段的信息。

以下代码示例使用 MemberInfo 来列出 System.IO.File 类中的成员数量并使用 System.Type.IsPublic 属性来确定该类的可见性。
[C#]
using System;
using System.IO;
using System.Reflection;
class Mymemberinfo
{
public static void Main(string[] args)
{
 Console.WriteLine ("\nReflection.MemberInfo");
 // Get the Type and MemberInfo.
 Type MyType =Type.GetType("System.IO.File");
 MemberInfo[] Mymemberinfoarray = MyType.GetMembers();
 // Get and display the DeclaringType method.
 Console.WriteLine("\nThere are {0} members in {1}.",
Mymemberinfoarray.Length, MyType.FullName);
 Console.WriteLine("{0}.", MyType.FullName);
 if (MyType.IsPublic)
 {
Console.WriteLine("{0} is public.", MyType.FullName);
}
 }
}


以下代码示例调查指定成员的类型。它对 MemberInfo 类的一个成员执行反射,然后列出其类型。
[C#]
// This code displays information about the GetValue method of FieldInfo.
using System;
using System.Reflection;
class MyMethodInfo {
 public static int Main() {
Console.WriteLine("Reflection.MethodInfo");
// Get and display the Type.
Type MyType = Type.GetType("System.Reflection.FieldInfo");
// Specify the member for which you want type information here.
MethodInfo Mymethodinfo = MyType.GetMethod("GetValue");
Console.WriteLine(MyType.FullName + "." + Mymethodinfo.Name);
// Get and display the MemberType property.
MemberTypes Mymembertypes = Mymethodinfo.MemberType;
if (MemberTypes.Constructor == Mymembertypes) {
Console.WriteLine("MemberType is of type All");
}
else if (MemberTypes.Custom == Mymembertypes) {
 Console.WriteLine("MemberType is of type Custom");
}
else if (MemberTypes.Event == Mymembertypes) {
 Console.WriteLine("MemberType is of type Event");
}
else if (MemberTypes.Field == Mymembertypes) {
 Console.WriteLine("MemberType is of type Field");
}
else if (MemberTypes.Method == Mymembertypes) {
 Console.WriteLine("MemberType is of type Method");
}
else if (MemberTypes.Property == Mymembertypes) {
 Console.WriteLine("MemberType is of type Property");
}
else if (MemberTypes.TypeInfo == Mymembertypes) {
 Console.WriteLine("MemberType is of type TypeInfo");
}
return 0;
 }
}


以下示例代码使用所有的反射 *Info 类以及 BindingFlags 来列出指定类的所有成员(构造函数、字段、属性、事件和方法),并将这些成员划分为静态和实例类别。
[C#]
// This program lists all the members of the
// System.IO.BufferedStream class.
using System;
using System.IO;
using System.Reflection;
class ListMembers {
 public static void Main(String[] args) {
// Specify the class here.
Type t = typeof (System.IO.BufferedStream);
Console.WriteLine ("Listing all the members (public and non public) of the {0} type", t);
// Static Fields are listed first.
FieldInfo [] fi = t.GetFields (BindingFlags.Static

关键词:在.NET运行时知道分类信息(1) Paul_Ni(原作)




Copyright © 2012-2018 争怎路由网(http://www.zhengzen.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版