From 6eb915c129fc90c6f4c82ae097dd6ffad5239efc Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Jan 2021 14:28:30 +0800 Subject: +scripts --- .../XMainClient/ProtoBuf/Meta/AttributeMap.cs | 117 +++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 Client/Assets/Scripts/XMainClient/ProtoBuf/Meta/AttributeMap.cs (limited to 'Client/Assets/Scripts/XMainClient/ProtoBuf/Meta/AttributeMap.cs') diff --git a/Client/Assets/Scripts/XMainClient/ProtoBuf/Meta/AttributeMap.cs b/Client/Assets/Scripts/XMainClient/ProtoBuf/Meta/AttributeMap.cs new file mode 100644 index 00000000..4fb2059b --- /dev/null +++ b/Client/Assets/Scripts/XMainClient/ProtoBuf/Meta/AttributeMap.cs @@ -0,0 +1,117 @@ +using System; +using System.Reflection; + +namespace ProtoBuf.Meta +{ + internal abstract class AttributeMap + { + public abstract Type AttributeType { get; } + + public abstract object Target { get; } + + private sealed class ReflectionAttributeMap : AttributeMap + { + public override object Target + { + get + { + return this.attribute; + } + } + + public override Type AttributeType + { + get + { + return this.attribute.GetType(); + } + } + + private readonly Attribute attribute; + + public override bool TryGet(string key, bool publicOnly, out object value) + { + foreach (MemberInfo memberInfo in Helpers.GetInstanceFieldsAndProperties(this.attribute.GetType(), publicOnly)) + { + bool flag = string.Equals(memberInfo.Name, key, StringComparison.OrdinalIgnoreCase); + if (flag) + { + PropertyInfo propertyInfo = memberInfo as PropertyInfo; + bool flag2 = propertyInfo != null; + bool result; + if (flag2) + { + value = propertyInfo.GetValue(this.attribute, null); + result = true; + } + else + { + FieldInfo fieldInfo = memberInfo as FieldInfo; + bool flag3 = fieldInfo != null; + if (!flag3) + { + throw new NotSupportedException(memberInfo.GetType().Name); + } + value = fieldInfo.GetValue(this.attribute); + result = true; + } + return result; + } + } + value = null; + return false; + } + + public ReflectionAttributeMap(Attribute attribute) + { + this.attribute = attribute; + } + } + + [Obsolete("Please use AttributeType instead")] + public new Type GetType() + { + return this.AttributeType; + } + + public abstract bool TryGet(string key, bool publicOnly, out object value); + + public bool TryGet(string key, out object value) + { + return this.TryGet(key, true, out value); + } + + public static AttributeMap[] Create(TypeModel model, Type type, bool inherit) + { + object[] customAttributes = type.GetCustomAttributes(inherit); + AttributeMap[] array = new AttributeMap[customAttributes.Length]; + for (int i = 0; i < customAttributes.Length; i++) + { + array[i] = new AttributeMap.ReflectionAttributeMap((Attribute)customAttributes[i]); + } + return array; + } + + public static AttributeMap[] Create(TypeModel model, MemberInfo member, bool inherit) + { + object[] customAttributes = member.GetCustomAttributes(inherit); + AttributeMap[] array = new AttributeMap[customAttributes.Length]; + for (int i = 0; i < customAttributes.Length; i++) + { + array[i] = new AttributeMap.ReflectionAttributeMap((Attribute)customAttributes[i]); + } + return array; + } + + public static AttributeMap[] Create(TypeModel model, Assembly assembly) + { + object[] customAttributes = assembly.GetCustomAttributes(false); + AttributeMap[] array = new AttributeMap[customAttributes.Length]; + for (int i = 0; i < customAttributes.Length; i++) + { + array[i] = new AttributeMap.ReflectionAttributeMap((Attribute)customAttributes[i]); + } + return array; + } + } +} -- cgit v1.1-26-g67d0