using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Linq;
|
using System.Reflection;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace OpenTapEditor.Util
|
{
|
public static class PropertyInfoExtensions
|
{
|
///// <summary>
|
///// 判断属性是否只读
|
///// </summary>
|
///// <param name="property">要检查的属性</param>
|
///// <param name="considerInitOnlyAsReadOnly">是否将init-only属性视为只读</param>
|
///// <param name="considerNonPublicSetterAsReadOnly">是否将非公共setter视为只读</param>
|
///// <returns>true如果属性是只读的</returns>
|
//public static bool IsReadOnly(
|
// this PropertyInfo property,
|
// bool considerInitOnlyAsReadOnly = true,
|
// bool considerNonPublicSetterAsReadOnly = false)
|
//{
|
// // 基本检查:没有setter就是只读
|
// if (property.SetMethod == null)
|
// return true;
|
|
// // 检查是否是init-only属性
|
// if (considerInitOnlyAsReadOnly && property.IsInitOnly())
|
// return true;
|
|
// // 检查setter的可访问性
|
// if (considerNonPublicSetterAsReadOnly && !property.SetMethod.IsPublic)
|
// return true;
|
|
// // 检查是否有只读特性标记
|
// if (HasReadOnlyAttribute(property))
|
// return true;
|
|
// return false;
|
//}
|
|
//private static bool HasReadOnlyAttribute(PropertyInfo property)
|
//{
|
// return property.GetCustomAttribute<ReadOnlyAttribute>()?.IsReadOnly == true ||
|
// property.GetCustomAttribute<System.ComponentModel.ReadOnlyAttribute>()?.IsReadOnly == true;
|
//}
|
}
|
}
|