//----------------------------------------------------------------------- // // Copyright (c) 2018 Sirenix IVS // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // //----------------------------------------------------------------------- namespace VRC.Udon.Serialization.OdinSerializer.Utilities { using System; using System.Globalization; using System.Reflection; /// /// Provides a methods of representing imaginary fields which are unique to serialization. /// /// We aggregate the FieldInfo associated with this member and return a mangled form of the name. /// /// public sealed class MemberAliasFieldInfo : FieldInfo { /// /// The default fake name separator string. /// private const string FAKE_NAME_SEPARATOR_STRING = "+"; private FieldInfo aliasedField; private string mangledName; /// /// Initializes a new instance of the class. /// /// The field to alias. /// The name prefix to use. public MemberAliasFieldInfo(FieldInfo field, string namePrefix) { this.aliasedField = field; this.mangledName = string.Concat(namePrefix, FAKE_NAME_SEPARATOR_STRING, this.aliasedField.Name); } /// /// Initializes a new instance of the class. /// /// The field to alias. /// The name prefix to use. /// The separator string to use. public MemberAliasFieldInfo(FieldInfo field, string namePrefix, string separatorString) { this.aliasedField = field; this.mangledName = string.Concat(namePrefix, separatorString, this.aliasedField.Name); } /// /// Gets the aliased field. /// /// /// The aliased field. /// public FieldInfo AliasedField { get { return this.aliasedField; } } /// /// Gets the module in which the type that declares the member represented by the current is defined. /// public override Module Module { get { return this.aliasedField.Module; } } /// /// Gets a value that identifies a metadata element. /// public override int MetadataToken { get { return this.aliasedField.MetadataToken; } } /// /// Gets the name of the current member. /// public override string Name { get { return this.mangledName; } } /// /// Gets the class that declares this member. /// public override Type DeclaringType { get { return this.aliasedField.DeclaringType; } } /// /// Gets the class object that was used to obtain this instance of MemberInfo. /// public override Type ReflectedType { get { return this.aliasedField.ReflectedType; } } /// /// Gets the type of the field. /// /// /// The type of the field. /// public override Type FieldType { get { return this.aliasedField.FieldType; } } /// /// Gets a RuntimeFieldHandle, which is a handle to the internal metadata representation of a field. /// public override RuntimeFieldHandle FieldHandle { get { return this.aliasedField.FieldHandle; } } /// /// Gets the attributes. /// /// /// The attributes. /// public override FieldAttributes Attributes { get { return this.aliasedField.Attributes; } } /// /// When overridden in a derived class, returns an array of all custom attributes applied to this member. /// /// True to search this member's inheritance chain to find the attributes; otherwise, false. This parameter is ignored for properties and events; see Remarks. /// /// An array that contains all the custom attributes applied to this member, or an array with zero elements if no attributes are defined. /// public override object[] GetCustomAttributes(bool inherit) { return this.aliasedField.GetCustomAttributes(inherit); } /// /// When overridden in a derived class, returns an array of custom attributes applied to this member and identified by . /// /// The type of attribute to search for. Only attributes that are assignable to this type are returned. /// True to search this member's inheritance chain to find the attributes; otherwise, false. This parameter is ignored for properties and events; see Remarks. /// /// An array of custom attributes applied to this member, or an array with zero elements if no attributes assignable to have been applied. /// public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return this.aliasedField.GetCustomAttributes(attributeType, inherit); } /// /// When overridden in a derived class, indicates whether one or more attributes of the specified type or of its derived types is applied to this member. /// /// The type of custom attribute to search for. The search includes derived types. /// True to search this member's inheritance chain to find the attributes; otherwise, false. This parameter is ignored for properties and events; see Remarks. /// /// True if one or more instances of or any of its derived types is applied to this member; otherwise, false. /// public override bool IsDefined(Type attributeType, bool inherit) { return this.aliasedField.IsDefined(attributeType, inherit); } /// /// Gets the value of the field. /// /// The object instance to get the value from. /// The value of the field. public override object GetValue(object obj) { return this.aliasedField.GetValue(obj); } /// /// When overridden in a derived class, sets the value of the field supported by the given object. /// /// The object whose field value will be set. /// The value to assign to the field. /// A field of Binder that specifies the type of binding that is desired (for example, Binder.CreateInstance or Binder.ExactBinding). /// A set of properties that enables the binding, coercion of argument types, and invocation of members through reflection. If is null, then Binder.DefaultBinding is used. /// The software preferences of a particular culture. public override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture) { this.aliasedField.SetValue(obj, value, invokeAttr, binder, culture); } } }