当前位置:网站首页>获取属性特性几种方法
获取属性特性几种方法
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身份验证包自启动机制

高精度加法

HRnet

【图像分类】2022-CycleMLP ICLR

Example 043: Scope, class methods and variables

Difference Between Data Mining and Data Warehousing

Robust Real-time LiDAR-inertial Initialization (Real-time Robust LiDAR Inertial Initialization) Paper Learning

flex 的 三个参数:flex-grow、flex-shrink、flex-basis

使用curl指令发起websocket请求

湖仓一体电商项目(四):项目数据种类与采集
随机推荐
2022.8.9 Exam arrangement and transformation--1200 questions solution
Write a drop-down refresh component
【Kali安全渗透测试实践教程】第9章 无线网络渗透
QT modal dialog and non-modal dialog learning
关于redis在业务中的应用问题,如何解决?
what is a microcontroller or mcu
What is a Cross-Site Request Forgery (CSRF) attack?How to defend?
NFG电商系统在元宇宙趋势下做什么?
storage of data in memory
跨站请求伪造(CSRF)攻击是什么?如何防御?
网路编程_调试accept
官宣出自己的博客啦
[Semantic Segmentation] 2022-HRViT CVPR
HACKTHEBOX——Bank
将信号与不同开始时间对齐
芯片加速器 Accelerator
Chapter 21 Source Code File REST API Reference (3)
2022.8.8考试游记总结
从滑动标尺模型看企业网络安全能力评估与建设
量化投资学习——在FPGA上运行高频交易策略