|
| 1 | +#region -- License Terms -- |
| 2 | +// |
| 3 | +// NLiblet |
| 4 | +// |
| 5 | +// Copyright (C) 2015 FUJIWARA, Yusuke |
| 6 | +// |
| 7 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +// you may not use this file except in compliance with the License. |
| 9 | +// You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, software |
| 14 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +// See the License for the specific language governing permissions and |
| 17 | +// limitations under the License. |
| 18 | +// |
| 19 | +#endregion -- License Terms -- |
| 20 | + |
| 21 | +using System; |
| 22 | +using System.ComponentModel; |
| 23 | +using System.Linq; |
| 24 | +using System.Reflection; |
| 25 | + |
| 26 | +namespace MsgPack.Serialization.Reflection |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// <strong>This is intened to MsgPack for CLI internal use. Do not use this type from application directly.</strong> |
| 30 | + /// Defines serialization helper APIs. |
| 31 | + /// </summary> |
| 32 | + [EditorBrowsable( EditorBrowsableState.Never )] |
| 33 | + public static class ReflectionHelpers |
| 34 | + { |
| 35 | + internal static readonly MethodInfo GetRuntimeMethodMethod = |
| 36 | + FromExpression.ToMethod( |
| 37 | + ( Type source, string name, Type[] parameterTypes ) => GetRuntimeMethod( source, name, parameterTypes ) |
| 38 | + ); |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Gets a specific method even if the candidate is not public. |
| 42 | + /// </summary> |
| 43 | + /// <param name="source">The target type.</param> |
| 44 | + /// <param name="name">The name of the method.</param> |
| 45 | + /// <param name="parameterTypes">The types of the parameter.</param> |
| 46 | + /// <returns>A <see cref="MethodInfo"/> if found; otherwise, <c>null</c>.</returns> |
| 47 | + /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception> |
| 48 | + /// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception> |
| 49 | + [EditorBrowsable( EditorBrowsableState.Never )] |
| 50 | + public static MethodInfo GetRuntimeMethod( this Type source, string name, params Type[] parameterTypes ) |
| 51 | + { |
| 52 | + if ( name == null ) |
| 53 | + { |
| 54 | + throw new ArgumentNullException( "name" ); |
| 55 | + } |
| 56 | + |
| 57 | + if ( name.Length == 0 ) |
| 58 | + { |
| 59 | + throw new ArgumentException( "'name' cannot be empty.", "name" ); |
| 60 | + } |
| 61 | + |
| 62 | + var safeParameterTypes = parameterTypes ?? ReflectionAbstractions.EmptyTypes; |
| 63 | + |
| 64 | + return |
| 65 | + source.GetRuntimeMethods() |
| 66 | + .SingleOrDefault( |
| 67 | + m => m.Name == name && m.GetParameters().Select( p => p.ParameterType ).SequenceEqual( safeParameterTypes ) |
| 68 | + ); |
| 69 | + } |
| 70 | + internal static readonly MethodInfo GetRuntimeFieldMethod = |
| 71 | + FromExpression.ToMethod( |
| 72 | + ( Type source, string name ) => GetRuntimeField( source, name ) |
| 73 | + ); |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Gets a specific field even if the candidate is not public. |
| 77 | + /// </summary> |
| 78 | + /// <param name="source">The target type.</param> |
| 79 | + /// <param name="name">The name of the method.</param> |
| 80 | + /// <returns>A <see cref="FieldInfo"/> if found; otherwise, <c>null</c>.</returns> |
| 81 | + /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception> |
| 82 | + /// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception> |
| 83 | + [EditorBrowsable( EditorBrowsableState.Never )] |
| 84 | + public static FieldInfo GetRuntimeField( this Type source, string name ) |
| 85 | + { |
| 86 | + if ( name == null ) |
| 87 | + { |
| 88 | + throw new ArgumentNullException( "name" ); |
| 89 | + } |
| 90 | + |
| 91 | + if ( name.Length == 0 ) |
| 92 | + { |
| 93 | + throw new ArgumentException( "'name' cannot be empty.", "name" ); |
| 94 | + } |
| 95 | + |
| 96 | + return |
| 97 | + source.GetRuntimeFields() |
| 98 | + .SingleOrDefault( |
| 99 | + m => m.Name == name |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments