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

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

            return(false);
        }
예제 #2
0
        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);
        }
예제 #3
0
        public static object DuckAs(this object instance, Type targetType)
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            if (targetType != null && (targetType.IsPublic || targetType.IsNestedPublic))
            {
                var proxyResult = DuckType.GetOrCreateProxyType(targetType, instance.GetType());
                if (proxyResult.Success)
                {
                    return(proxyResult.CreateInstance(instance));
                }
            }

            return(null);
        }
예제 #4
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 NET45
            if (!proxyType.IsPublic && !proxyType.IsNestedPublic)
            {
                DuckTypeTypeIsNotPublicException.Throw(proxyType, nameof(proxyType));
            }
#endif
        }
예제 #5
0
        public static bool TryDuckCast(this object instance, Type targetType, out object value)
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            if (targetType != null && (targetType.IsPublic || targetType.IsNestedPublic))
            {
                var proxyResult = DuckType.GetOrCreateProxyType(targetType, instance.GetType());
                if (proxyResult.Success)
                {
                    value = proxyResult.CreateInstance(instance);
                    return(true);
                }
            }

            value = default;
            return(false);
        }
예제 #6
0
        public static T DuckAs <T>(this object instance)
            where T : class
        {
            if (instance is null)
            {
                DuckTypeTargetObjectInstanceIsNull.Throw();
            }

            if (DuckType.CreateCache <T> .IsVisible)
            {
                var 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();
            }

            if (DuckType.CreateCache <T> .IsVisible)
            {
                var proxyResult = DuckType.CreateCache <T> .GetProxy(instance.GetType());

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

            value = default;
            return(false);
        }