예제 #1
0
        public static bool DuckIs <T>(this object instance)
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            return(DuckType.CanCreate <T>(instance));
        }
        private static void EnsureArguments(Type?proxyType, object?instance)
        {
            if (proxyType is null)
            {
                DuckTypeProxyTypeDefinitionIsNull.Throw();
            }

            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }
        }
예제 #3
0
        public static bool DuckIs(this object instance, Type targetType)
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            if (targetType != null)
            {
                return(DuckType.CanCreate(targetType, instance));
            }

            return(false);
        }
        public static bool DuckIs <T>(this object instance)
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            if (DuckType.CreateCache <T> .IsVisible)
            {
                return(DuckType.CanCreate <T>(instance));
            }

            return(false);
        }
예제 #5
0
        /// <summary>
        /// Checks and ensures the arguments for the Create methods
        /// </summary>
        /// <param name="proxyType">Duck type</param>
        /// <param name="instance">Instance value</param>
        /// <exception cref="ArgumentNullException">If the duck type or the instance value is null</exception>
        private static void EnsureArguments(Type proxyType, object instance)
        {
            if (proxyType is null)
            {
                DuckTypeProxyTypeDefinitionIsNull.Throw();
            }

            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            if (!proxyType.IsPublic && !proxyType.IsNestedPublic)
            {
                DuckTypeTypeIsNotPublicException.Throw(proxyType, nameof(proxyType));
            }
        }
예제 #6
0
        public static T DuckAs <T>(this object instance)
            where T : class
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            DuckType.CreateTypeResult proxyResult = DuckType.CreateCache <T> .GetProxy(instance.GetType());

            if (proxyResult.Success)
            {
                return(proxyResult.CreateInstance <T>(instance));
            }

            return(null);
        }
예제 #7
0
        public static bool TryDuckCast <T>(this object instance, out T value)
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            DuckType.CreateTypeResult proxyResult = DuckType.CreateCache <T> .GetProxy(instance.GetType());

            if (proxyResult.Success)
            {
                value = proxyResult.CreateInstance <T>(instance);
                return(true);
            }

            value = default;
            return(false);
        }
예제 #8
0
        public static object DuckAs(this object instance, Type targetType)
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            if (targetType != null)
            {
                DuckType.CreateTypeResult proxyResult = DuckType.GetOrCreateProxyType(targetType, instance.GetType());
                if (proxyResult.Success)
                {
                    return(proxyResult.CreateInstance(instance));
                }
            }

            return(null);
        }
예제 #9
0
        public static bool TryDuckCast(this object instance, Type targetType, out object value)
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            if (targetType != null)
            {
                DuckType.CreateTypeResult proxyResult = DuckType.GetOrCreateProxyType(targetType, instance.GetType());
                if (proxyResult.Success)
                {
                    value = proxyResult.CreateInstance(instance);
                    return(true);
                }
            }

            value = default;
            return(false);
        }
예제 #10
0
        public static bool TryDuckImplement(this object?instance, Type?typeToDeriveFrom, [NotNullWhen(true)] out object?value)
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            if (typeToDeriveFrom != null)
            {
                DuckType.CreateTypeResult proxyResult = DuckType.GetOrCreateReverseProxyType(typeToDeriveFrom, instance.GetType());
                if (proxyResult.Success)
                {
                    value = proxyResult.CreateInstance(instance);
                    return(true);
                }
            }

            value = default;
            return(false);
        }