Beispiel #1
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);
        }
Beispiel #2
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);
        }
Beispiel #3
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);
        }
Beispiel #4
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);
        }
Beispiel #5
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);
        }