//-----------------------------------------------------------------------
//
// 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 aliased methods.
///
/// In this case, what we're representing is a method on a parent class with the same name.
///
/// We aggregate the MethodInfo associated with this member and return a mangled form of the name.
/// The name that we return is "parentname+methodName".
///
///
public sealed class MemberAliasMethodInfo : MethodInfo
{
///
/// The default fake name separator string.
///
private const string FAKE_NAME_SEPARATOR_STRING = "+";
private MethodInfo aliasedMethod;
private string mangledName;
///
/// Initializes a new instance of the class.
///
/// The method to alias.
/// The name prefix to use.
public MemberAliasMethodInfo(MethodInfo method, string namePrefix)
{
this.aliasedMethod = method;
this.mangledName = string.Concat(namePrefix, FAKE_NAME_SEPARATOR_STRING, this.aliasedMethod.Name);
}
///
/// Initializes a new instance of the class.
///
/// The method to alias.
/// The name prefix to use.
/// The separator string to use.
public MemberAliasMethodInfo(MethodInfo method, string namePrefix, string separatorString)
{
this.aliasedMethod = method;
this.mangledName = string.Concat(namePrefix, separatorString, this.aliasedMethod.Name);
}
///
/// Gets the aliased method.
///
///
/// The aliased method.
///
public MethodInfo AliasedMethod { get { return this.aliasedMethod; } }
///
/// Gets the custom attributes for the return type.
///
public override ICustomAttributeProvider ReturnTypeCustomAttributes { get { return this.aliasedMethod.ReturnTypeCustomAttributes; } }
///
/// Gets a handle to the internal metadata representation of a method.
///
public override RuntimeMethodHandle MethodHandle { get { return this.aliasedMethod.MethodHandle; } }
///
/// Gets the attributes associated with this method.
///
public override MethodAttributes Attributes { get { return this.aliasedMethod.Attributes; } }
public override Type ReturnType { get { return this.aliasedMethod.ReturnType; } }
///
/// Gets the class that declares this member.
///
public override Type DeclaringType { get { return this.aliasedMethod.DeclaringType; } }
///
/// Gets the name of the current member.
///
public override string Name { get { return this.mangledName; } }
///
/// Gets the class object that was used to obtain this instance of MemberInfo.
///
public override Type ReflectedType { get { return this.aliasedMethod.ReflectedType; } }
///
/// When overridden in a derived class, returns the MethodInfo object for the method on the direct or indirect base class in which the method represented by this instance was first declared.
///
///
/// A MethodInfo object for the first implementation of this method.
///
public override MethodInfo GetBaseDefinition()
{
return this.aliasedMethod.GetBaseDefinition();
}
///
/// 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.aliasedMethod.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.aliasedMethod.GetCustomAttributes(attributeType, inherit);
}
///
/// When overridden in a derived class, returns the flags.
///
///
/// The MethodImplAttributes flags.
///
public override MethodImplAttributes GetMethodImplementationFlags()
{
return this.aliasedMethod.GetMethodImplementationFlags();
}
///
/// When overridden in a derived class, gets the parameters of the specified method or constructor.
///
///
/// An array of type ParameterInfo containing information that matches the signature of the method (or constructor) reflected by this MethodBase instance.
///
public override ParameterInfo[] GetParameters()
{
return this.aliasedMethod.GetParameters();
}
///
/// When overridden in a derived class, invokes the reflected method or constructor with the given parameters.
///
/// The object on which to invoke the method or constructor. If a method is static, this argument is ignored. If a constructor is static, this argument must be null or an instance of the class that defines the constructor.
/// A bitmask that is a combination of 0 or more bit flags from . If is null, this parameter is assigned the value ; thus, whatever you pass in is ignored.
/// An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects via reflection. If is null, the default binder is used.
/// An argument list for the invoked method or constructor. This is an array of objects with the same number, order, and type as the parameters of the method or constructor to be invoked. If there are no parameters, this should be null.If the method or constructor represented by this instance takes a ByRef parameter, there is no special attribute required for that parameter in order to invoke the method or constructor using this function. Any object in this array that is not explicitly initialized with a value will contain the default value for that object type. For reference-type elements, this value is null. For value-type elements, this value is 0, 0.0, or false, depending on the specific element type.
/// An instance of CultureInfo used to govern the coercion of types. If this is null, the CultureInfo for the current thread is used. (This is necessary to convert a String that represents 1000 to a Double value, for example, since 1000 is represented differently by different cultures.)
///
/// An Object containing the return value of the invoked method, or null in the case of a constructor, or null if the method's return type is void. Before calling the method or constructor, Invoke checks to see if the user has access permission and verifies that the parameters are valid.CautionElements of the array that represent parameters declared with the ref or out keyword may also be modified.
///
public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
{
return this.aliasedMethod.Invoke(obj, invokeAttr, binder, parameters, culture);
}
///
/// 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.aliasedMethod.IsDefined(attributeType, inherit);
}
}
}