Exemplo n.º 1
0
        /// <summary>Gets the cast needed for casting to the database type.</summary>
        /// <exception cref="InvalidCastException">
        /// If the required cast is not defined.
        /// </exception>
        private static MethodInfo GetCast(Type sourceType, SingleValueObjectAttribute attr)
        {
            if (!Casts.TryGetValue(sourceType, out MethodInfo cast))
            {
                var returnType = attr.DatabaseType ?? attr.UnderlyingType;
                var methods    = sourceType.GetMethods(BindingFlags.Public | BindingFlags.Static)
                                 .Where(m =>
                                        m.IsHideBySig &&
                                        m.IsSpecialName &&
                                        m.GetParameters().Length == 1 &&
                                        m.ReturnType == returnType)
                                 .ToList();

                if (methods.Any())
                {
                    cast = methods[0];
                    Casts[sourceType] = cast;
                }
                else
                {
                    throw new InvalidCastException(string.Format(QowaivMessages.InvalidCastException_FromTo, sourceType, returnType));
                }
            }
            return(cast);
        }