CSharp: Reflection in donet 6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GeovinduDu
{
[AttributeUsage(
AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)]
public class HelpAttribute : Attribute
{
///// <summary>
/////
///// </summary>
///// <param name="Description_in"></param>
//public HelpAttribute(String Description_in, string author)
//{
// this.description = Description_in;
// this.author = author;
//}
/// <summary>
///
/// </summary>
/// <param name="Description_in"></param>
/// <param name="author"></param>
/// <param name="bg"></param>
/// <param name="dev"></param>
/// <param name="d"></param>
public HelpAttribute(String Description_in, string author, int bg, string dev, string d)
{
this.description = Description_in;
this.author = author;
this.bugNo = bg;
this.developer = dev;
this.lastReview = d;
}
/// <summary>
///
/// </summary>
protected String description;
protected string author;
/// <summary>
///
/// </summary>
public String Description
{
get
{
return this.description;
}
}
/// <summary>
///
/// </summary>
public String Author
{
get
{
return this.author;
}
}
private int bugNo;
private string developer;
private string lastReview;
public string message;
///// <summary>
/////
///// </summary>
///// <param name="bg"></param>
///// <param name="dev"></param>
///// <param name="d"></param>
//public HelpAttribute(int bg, string dev, string d)
//{
// this.bugNo = bg;
// this.developer = dev;
// this.lastReview = d;
//}
/// <summary>
///
/// </summary>
public int BugNo
{
get
{
return bugNo;
}
}
/// <summary>
/// 开发者
/// </summary>
public string Developer
{
get
{
return developer;
}
}
/// <summary>
///
/// </summary>
public string LastReview
{
get
{
return lastReview;
}
}
/// <summary>
///
/// </summary>
public string Message
{
get
{
return message;
}
set
{
message = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using static System.Console;
namespace GeovinduDu
{
/// <summary>
///
/// </summary>
[Help("这是一门无所事事的对象","geovindu",1,"geovindu","涂聚文",message ="message")]
[Help("它包含一个什么都不做的方法", "geovindu",1, "geovindu", "涂聚文", message = "message")]
[Help(description:"geovindu",author:"geovindu",developer:"geovindu",bugNo:1,lastReview:"geovindu",message ="geovindu")]
public class geovindu
{
/// <summary>
///
/// </summary>
/// <param name="inputvalue"></param>
[Help("这是一种什么都不做的方法", "geovindu",2, "geovindu", "涂聚文", message = "message")]
[Help(description: "geovindu", author: "geovindu", developer: "geovindu", bugNo: 1, lastReview: "geovindu", message = "geovindu")]
public void DuMethod(string inputvalue)
{
WriteLine("geovindu,Geovin Du,涂聚文"+inputvalue);
}
}
}
using static System.Console;
using System.Reflection;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace GeovinduDu
{
/// <summary>
///
/// </summary>
public class Program
{
/// <summary>
///
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
int a = 10; // 00001010
int b = 6; // 00000110
WriteLine($"a = {a}");
WriteLine($"b = {b}");
WriteLine($"a & b = {a & b}"); // 2-bit column only
WriteLine($"a | b = {a | b}"); // 8, 4, and 2-bit columns
WriteLine($"a ^ b = {a ^ b}"); // 8 and 4-bit columns
string s = $"a ^ b = {a ^ b}".ToString();
WriteLine("geovindu:" + s);
// 01010000 left-shift a by three bit columns
WriteLine($"a << 3 = {a << 3}");
// multiply a by 8
WriteLine($"a * 8 = {a * 8}");
// 00000011 right-shift b by one bit column
WriteLine($"b >> 1 = {b >> 1}");
WriteLine();
WriteLine("Outputting integers as binary:");
WriteLine($"a = {ToBinaryString(a)}");
WriteLine($"b = {ToBinaryString(b)}");
WriteLine($"a & b = {ToBinaryString(a & b)}");
WriteLine($"a | b = {ToBinaryString(a | b)}");
WriteLine($"a ^ b = {ToBinaryString(a ^ b)}");
WriteLine();
Assembly? assembly = Assembly.GetEntryAssembly();
if (assembly == null) return;
// loop through the assemblies that this app references
foreach (AssemblyName name in assembly.GetReferencedAssemblies())
{
// load the assembly so we can read its details
Assembly Dugeovindu = Assembly.Load(name);
// declare a variable to count the number of methods
int methodCount = 0;
string methodname = "";
// loop through all the types in the assembly
foreach (TypeInfo t in Dugeovindu.DefinedTypes)
{
// add up the counts of methods
methodCount += t.GetMethods().Count();
}
foreach (var propertyInfo in Dugeovindu.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance))
{
methodname = propertyInfo.Name;
}
//output the count of types and their methods
// WriteLine("{0:N0} types with {1:N0} methods in {2} assembly {3}.", arg0: Dugeovindu.DefinedTypes.Count(), arg1: methodCount, arg2: name.Name);
WriteLine(name.Name);
}
System.Console.WriteLine();
System.Reflection.MemberInfo info = typeof(geovindu);
object[] attributes = info.GetCustomAttributes(true);
for (int i = 0; i < attributes.Length; i++)
{
System.Console.WriteLine(attributes[i]);
}
System.Console.WriteLine();
var Duattributes = Attribute.GetCustomAttributes(typeof(geovindu), typeof(HelpAttribute));
if (attributes.Any())
{
foreach (var attribute in Duattributes)
{
Console.WriteLine("Attribute Value: " + ((HelpAttribute)attribute).Description);
Console.WriteLine("Attribute Value: " + ((HelpAttribute)attribute).Author);
Console.WriteLine("Type ID: " + attribute.TypeId);
}
}
System.Console.WriteLine();
foreach (object attrib in info.GetCustomAttributes(true))
{
Console.WriteLine("Attribute Value: " + ((HelpAttribute)attrib).Description);
Console.WriteLine("Attribute Value: " + ((HelpAttribute)attrib).Author);
}
System.Console.WriteLine();
MethodInfo[] methodInfos = typeof(geovindu).GetMethods();
// writes all the property names
foreach (MethodInfo methodInfo in methodInfos)
{
Console.WriteLine(methodInfo.Name + "... ");
var values = methodInfo.GetParameters(); //方法中的参数
if (values != null)
{
foreach (ParameterInfo dus in values)
{
Console.WriteLine(dus.Name + ":" + dus.ParameterType.Name);
}
}
}
System.Console.WriteLine();
// 遍历方法特性
foreach (MethodInfo m in methodInfos)
{
foreach (Attribute du in m.GetCustomAttributes(true))
{
HelpAttribute geovin = du as HelpAttribute; //
if (null != geovin)
{
Console.WriteLine("Bug no: {0}, for Method: {1}",
geovin.BugNo, m.Name);
Console.WriteLine("Developer: {0}", geovin.Developer);
Console.WriteLine("Last Reviewed: {0}",
geovin.LastReview);
Console.WriteLine("Remarks: {0}", geovin.Message);
}
}
}
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
static string ToBinaryString(int value)
{
return Convert.ToString(value, toBase: 2).PadLeft(8, '0');
}
}
}
输出:
a = 10 b = 6 a & b = 2 a | b = 14 a ^ b = 12 geovindu:a ^ b = 12 a << 3 = 80 a * 8 = 80 b >> 1 = 3 Outputting integers as binary: a = 00001010 b = 00000110 a & b = 00000010 a | b = 00001110 a ^ b = 00001100 System.Runtime System.Console System.Linq GeovinduDu.HelpAttribute GeovinduDu.HelpAttribute GeovinduDu.HelpAttribute Attribute Value: 这是一门无所事事的对象 Attribute Value: geovindu Type ID: GeovinduDu.HelpAttribute Attribute Value: 它包含一个什么都不做的方法 Attribute Value: geovindu Type ID: GeovinduDu.HelpAttribute Attribute Value: geovindu Attribute Value: geovindu Type ID: GeovinduDu.HelpAttribute Attribute Value: 这是一门无所事事的对象 Attribute Value: geovindu Attribute Value: 它包含一个什么都不做的方法 Attribute Value: geovindu Attribute Value: geovindu Attribute Value: geovindu DuMethod... inputvalue:String GetType... ToString... Equals... obj:Object GetHashCode... Bug no: 2, for Method: DuMethod Developer: geovindu Last Reviewed: 涂聚文 Remarks: message Bug no: 1, for Method: DuMethod Developer: geovindu Last Reviewed: geovindu Remarks: geovindu
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
浙公网安备 33010602011771号