sách gpt4 ai đã đi

c# - 查看/检索属性

In lại 作者:行者123 更新时间:2023-11-30 15:40:14 27 4
mua khóa gpt4 Nike

在一个类中,我有以下代码:

/// 
[System.Xml.Serialization.XmlElementAttribute("Errors", typeof(ErrorsType))]
[System.Xml.Serialization.XmlElementAttribute("Success", typeof(SuccessType))]
[System.Xml.Serialization.XmlElementAttribute("Warnings", typeof(WarningsType))]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}

仅使用反射,是否可以检索这些属性?我在相应的 Kiểu 上看到了“GetCustomAttributes(),但并没有得到太多的快乐。

1 Câu trả lời

您需要从属性中检索属性,而不是类型本身,如下所示:

typeof(MyClass).GetProperty("Items").GetCustomAttributes(typeof(XmlElementAttribute), false);

或更完整(记得导入 System.Linq 以便 Cast<> 和 ToArray() 工作):

XmlElementAttribute[] attribs = typeof(TheType)
.GetProperty("Items")
.GetCustomAttributes(typeof(XmlElementAttribute), false)
.Cast()
.ToArray();

关于c# - 查看/检索属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9501346/

27 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com