当前位置:网站首页>获取属性特性几种方法
获取属性特性几种方法
2022-08-10 02:43:00 【zzyzxb】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace ConsoleApp3
{
internal class Program
{
public EventHandler<EventArgs> OnClick;
static void Main(string[] args)
{
Person person = new Person();
person.Name = "aoteman";
string s = GetDecription();
//AA();
//BB();
//CC();
//Console.WriteLine(ss);
Console.ReadLine();
}
public static string GetDecription()
{
Person person = new Person();
var field = person.GetType().GetProperty("Name");
var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));
return customAttribute == null ? person.ToString() : ((DescriptionAttribute)customAttribute).Description;
//string str = "";
//Desc desc = new Desc();
//PropertyDescriptor pd = TypeDescriptor.GetProperties(typeof(Desc))["des"];
//DescriptionAttribute description = pd == null ? null : pd.Attributes[typeof(DescriptionAttribute)] as DescriptionAttribute;
//str = description == null ? "" : description.Description;
string s = person.GetType().GetProperty("Name").GetGetMethod().Invoke(person, null).ToString();
}
public static void AA()
{
string str = "";
PropertyInfo pi = typeof(Desc).GetProperty("des");
foreach (object obj in pi.GetCustomAttributes(false))
{
if (obj is DescriptionAttribute)
{
str = (obj as DescriptionAttribute).Description;
}
}
}
public static void BB()
{
string str = "";
Desc desc = new Desc();
AttributeCollection attributes = TypeDescriptor.GetAttributes(desc);
DescriptionAttribute da = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
if (attributes.Contains(da))
{
str = da.Description;
}
}
public static void CC()
{
string str = "";
Type myType = typeof(Desc);
foreach (object obj in myType.GetCustomAttributes(false))
{
if (obj is DescriptionAttribute)
{
str = (obj as DescriptionAttribute).Description;
}
}
}
public static void ShowPerson(Person person)
{
int i = person?.Age ?? 0;
}
}
class Person
{
[Description("this is name"), Category("Layout")]
public string Name {
get; set; }
[Description("this is age"), Category("Layout")]
public int Age {
get; set; }
}
public struct Desc
{
private byte val1;
[Description("一个属性")]
public byte des
{
get {
return val1; }
set {
val1 = value; }
}
}
}
边栏推荐
猜你喜欢
【红队】ATT&CK - 自启动 - 利用LSA身份验证包自启动机制
HackTheBox——Beep
[Kali Security Penetration Testing Practice Course] Chapter 8 Web Penetration
[Kali Security Penetration Testing Practice Course] Chapter 7 Privilege Escalation
“双枪”木马病毒的进化史
论文理解:“PIAT: Physics Informed Adversarial Training for Solving Partial Differential Equations“
Example 046: Breaking the Cycle
使用curl指令发起websocket请求
Instance 042: Variable scope
(十四)时间延时任务及定时任务
随机推荐
GDB之指令基础参数
2022.8.8考试清洁工老马(sweeper)题解
官宣出自己的博客了
2022.8.9考试独特的投标拍卖--800题解
flutter 制作嵌套列表
Arrays类
2022年立下的flag完成情况
官宣出自己的博客啦
基于误差状态的卡尔曼滤波ESKF
【图像分类】2022-ConvMixer ICLR
实测办公场景下,国产远程控制软件的表现力如何?(技术解析)
【图像分类】2022-ResMLP
(十四)时间延时任务及定时任务
2022.8.8 Exam written in memory (memory)
PC摄像头设置 默认摄像头设置 win11 默认摄像头设置
实例046:打破循环
官宣出自己的博客啦
what is eabi
Example 046: Breaking the Cycle
2020.11.22考试哥德巴赫猜想题解